Reputation: 3074
Id like to reference a property in my .aspx from my .ascx.
Lets say my page class is:
public partial class MyAdminPage : System.Web.UI.Page
I was expecting I could do from my .ascx:
MyAdminPage myPageInstance = (MyAdminPage)this.Page;
or
MyAdminPage myPageInstance = this.Page as MyAdminPage;
Which I cant.
How could I reference my page class from the .ascx?
Update 1
Thanks for the replies.
I forgot to mention that the .aspx has a master page, and within the contentPlaceholder is where I have the .ascx control.
So from my .ascx if I do:
this.Parent --> I get a reference to "ContentPlaceHolder"
this.Parent.Parent --> I get a reference to "HtmlForm"
this.Parent.Parent --> reference to a 'MasterPage'.
I will not reuse the .ascx in any other .aspx pages just the one.
Upvotes: 1
Views: 3803
Reputation: 138
You could also use MyAdminPage myPageInstance = (MyAdminPage)this.Page;
Upvotes: 0
Reputation: 6032
sJohnny's comment is spot on, you should not be trying to modify parent (page) properties from the child (control). One good way to handle this situation is for the child to raise an event when it needs the parent to act. That way the parent can simply subscribe to the event and call whatever properties it needs at that time. This way your user control is in no way tied to the implementation of this particular page.
Upvotes: 4
Reputation: 176956
MyAdminPage myPageInstance =
(MyAdminPage)this.parent;//in you ascx will do the task
also check : Using parent page properties in user control
Upvotes: 1
Reputation: 109037
You can try
MyAdminPage myPageInstance = this.Parent as MyAdminPage;
if(myPageInstance != null)
{
...
}
Upvotes: 2