简体   繁体   中英

Can I use parent(web page) property in web user control?

How can I use parent(web page) property in web user control?

One more question:- How can I access the shared class(ie class in App_Code folder) property in web user control.

Thanks

You can use this.Page in asp.net user control to refer to page and it will always give you page in which that control is added.

You can access any public class declared in App_code folder directly in any user control without problem. Be careful of namespace and make sure to compile your project if you are having issues to access the class.

You could but I am not sure it would be a clean / full OOP approach, how about setting a public property or calling a public method of your user control from the page passing to it the value you need to use in the control?

this because the page hosting the control should be generic and is the page which contains the control not the other way round.

If this does not fit you, then you can take the control's Page property and cast it to the class of your page then you will be able to access its property but this will make your control specific instead of generic and it will only work when the control is hosted in pages of that exact type/class.

You have to mark the property as Public.

var myVar = ((ParentPageClass)this.Page).YourProperty;

To access the shared class you have to specify the namespace of that class:

YourProject.SomeNamespace.YourClass

or to include the namespace in your .ascx.cs file

using YourProject.SomeNamespace;

It's a cleaner aproach to pass the parameter to the user control from the parent page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM