user259286
user259286

Reputation: 1005

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

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!

Upvotes: 0

Views: 1604

Answers (3)

Łukasz Wiatrak
Łukasz Wiatrak

Reputation: 2767

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.

Upvotes: 1

Martyn
Martyn

Reputation: 1476

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.

Upvotes: 0

jdross
jdross

Reputation: 1206

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

Upvotes: 1

Related Questions