C# ASPX: How to get a Parent page's inherited property in a Control?

1.5k views Asked by At

I have an aspx page that inherits a master page which has a protected property. Like this:

masterpage { protected string propX.. }

MyPage : masterpage

---myControl:UserControl

In myControl code-behind I'd like to access propX

Any ideas?

Thanks!

3

There are 3 answers

0
Łukasz Wiatrak On BEST ANSWER

Maybe try to cast the Page property of myControl class to MyPage class?

string value = ((MyPage)this.Page).propX

And if you want to access this property from other class (like myControl), the access modifier of property propX should be set to internal or public

I've assumed, that you've placed myControl object on MyPage page.

0
jdross On

You could possibly change the access modifiers for the string. Maybe set it to internal.

2
Martyn On

Are you sure you're inheriting from the master page? Adding the MasterPage directive doesn't mean that it inherits from it. Usually an aspx page should directly or indirectly inherit from System.Web.UI.Page.

The master pages aren't "inherited" which means that protected members cannot be accessed from the page class (or control class). Your best option is to make the property public or internal.