how to access master page control from content page

asp.netMaster PagesCode Behind

asp.net Problem Overview


I have a master page which contains a label for status messages. I need to set the status text from different .aspx pages. How can this be done from the content page?

public partial class Site : System.Web.UI.MasterPage
{
    public string StatusNachricht
    {
        get
        {
            return lblStatus.Text;
        }
        set
        {
            lblStatus.Text = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {            
                   
    }
}

I have tried this, but was unsuccessful in making it work:

public partial class DatenAendern : System.Web.UI.Page
{
    var master = Master as Site;

    protected void Page_Load(object sender, EventArgs e)
    {               
        if (master != null)
        {
            master.setStatusLabel("");
        }
    }        

    protected void grdBenutzer_RowCommand(object sender, GridViewCommandEventArgs e)
    {           
            try
            {
                //some code
             
                if (master != null)
                {
                    master.setStatusLabel("Passwort erfolgreich geändert.");
                }
            }
            catch (Exception ex)
            {
                if (master != null)
                {
                    master.setStatusLabel("Passwort konnte nicht geändert werden!");
                }                                       
            }
        }
    }                   
}

asp.net Solutions


Solution 1 - asp.net

In the MasterPage.cs file add the property of Label like this:

public string ErrorMessage
{
    get
    {
        return lblMessage.Text;
    }
    set
    {
        lblMessage.Text = value;
    }
}

On your aspx page, just below the Page Directive add this:

<%@ Page Title="" Language="C#" MasterPageFile="Master Path Name"..... %>
<%@ MasterType VirtualPath="Master Path Name" %>   // Add this

And in your codebehind(aspx.cs) page you can then easily access the Label Property and set its text as required. Like this:

this.Master.ErrorMessage = "Your Error Message here";

Solution 2 - asp.net

In Content page you can access the label and set the text such as

Here 'lblStatus' is the your master page label ID

Label lblMasterStatus = (Label)Master.FindControl("lblStatus");

lblMasterStatus.Text = "Meaasage from content page";

Solution 3 - asp.net

It Works

To find master page controls on Child page

Label lbl_UserName = this.Master.FindControl("lbl_UserName") as Label;                    
lbl_UserName.Text = txtUsr.Text;

Solution 4 - asp.net

I have a helper method for this in my System.Web.UI.Page class

protected T FindControlFromMaster<T>(string name) where T : Control
{
     MasterPage master = this.Master;
     while (master != null)
     {
         T control = master.FindControl(name) as T;
         if (control != null)
             return control;

         master = master.Master;
     }
     return null;
}

then you can access using below code.

Label lblStatus = FindControlFromMaster<Label>("lblStatus");
if(lblStatus!=null) 
    lblStatus.Text = "something";

Solution 5 - asp.net

You cannot use var in a field, only on local variables.

But even this won't work:

Site master = Master as Site;

Because you cannot use this in a field and Master as Site is the same as this.Master as Site. So just initialize the field from Page_Init when the page is fully initialized and you can use this:

Site master = null;

protected void Page_Init(object sender, EventArgs e)
{            
    master = this.Master as Site;
}

Solution 6 - asp.net

This is more complicated if you have a nested MasterPage. You need to first find the content control that contains the nested MasterPage, and then find the control on your nested MasterPage from that.

Crucial bit: Master.Master.

See here: http://forums.asp.net/t/1059255.aspx?Nested+master+pages+and+Master+FindControl

Example:

> 'Find the content control
> > Dim ct As ContentPlaceHolder = Me.Master.Master.FindControl("cphMain") > > 'now find controls inside that content
> > Dim lbtnSave As LinkButton = ct.FindControl("lbtnSave")

Solution 7 - asp.net

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLeonidasFettView Question on Stackoverflow
Solution 1 - asp.netPraveen NambiarView Answer on Stackoverflow
Solution 2 - asp.netDotNet TeamView Answer on Stackoverflow
Solution 3 - asp.netshwetaView Answer on Stackoverflow
Solution 4 - asp.netsuryaView Answer on Stackoverflow
Solution 5 - asp.netTim SchmelterView Answer on Stackoverflow
Solution 6 - asp.netResourceView Answer on Stackoverflow
Solution 7 - asp.netJames HefferView Answer on Stackoverflow