Search code examples
asp.netattributesnestedmaster-pagesweb-controls

asp.net how to change a control which is in parent master page, from content page with nested master pages


I have ParentMaster.master, Child1Master.master, Child2Master.master and several content pages under both 2 child masters.
In the parent master I have controls (buttons and labels) residing outside of the asp:ContentPlaceHolder.
I need to change the text of the buttons and labels in the ParentMaster (2 levels up) from within the code behind of the content pages. I also need to change the 'visible' attribute of these controls to false or true.

The only thing I managed up to now was to change (from a content page) the text of a label in a 1-level Master, say from 'AAAA' to 'BBBB', like this :

In Master1.Master :

<li><asp:label ID="lbl_something" runat="server" Text="AAAA"></asp:label></li>  

In Master1.Master.cs :

public string str_In_Master
{
    get
    {
        return lbl_something.Text;
    }
    set
    {
        lbl_something.Text = value;
    }
    }

In the content page :

protected void Page_Load(object sender, EventArgs e)
{
   this.Master.str_In_Master = "BBBB";
}

But this wouldn't work in a nested master pages situations.

Can it be done at all? Can I change attributes of controls in the top-level MasterPage, including 'Text' and 'Visible'? Thank you!


Solution

  • you can access parent master from content page like this.

            var childMater1 = this.Master;
            var parentMater = childMater1.Master;
    

    u can access any public property of parent master.

            var childMater1 = this.Master;
            var parentMater = childMater1.Master;
            var typedMaster = parentMater as ParentMaster;
            typedMaster.SomeProperty = "some value";
    

    u can access any control of ParentMaster.

            var childMater1 = this.Master;
            var parentMater = childMater1.Master;
            var label = parentMater.FindControl("someLabelId") as Label;
            label.Text = "new label value";