简体   繁体   中英

How can I set a property on a master page from a custom control?

I have a master page.

I'm working on a testpage which inherits the master page.

the master page has a public property which is accessible to turn visibility on and off.

I have a set of controls which I can include into testpage. One of these controls needs to be able to set the visibility of a masterpage control.

Normally in a page code behind I would just say;

this.Master.ShowItem = false;

I have no idea how to be able to access this property from the custom control?

You just have to cast the master to the correct type. Assuming the type of your master is SiteMaster :

var master = this.Page.Master as SiteMaster;
if(master != null)  // cast failed, your master is a different type
{
    master.ShowItem = false;
}

So the navigation is:

(sry due to my reputation I cant make comments, so as an answer)

Hint: Make sure that the Property you want to access in the master page is set to public.

Heeding this the solution of Tim Schmelter works fine.

I don't believe you can set a property of master page directly. But you can find the control of the master page and made them visible/invisible from conternt page like this:

((Label)this.Page.Master.FindControl("IdOfTYurControl")).Visible = false;

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