58

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!");
                }                                       
            }
        }
    }                   
}
ArnonZ
  • 3,822
  • 4
  • 32
  • 42
LeonidasFett
  • 3,052
  • 4
  • 46
  • 76

7 Answers7

93

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";
JonH
  • 32,732
  • 12
  • 87
  • 145
Praveen Nambiar
  • 4,852
  • 1
  • 22
  • 31
  • 1
    change the title of this question to more relevant one. 'how to access master page control from content page' – Praveen Nambiar Mar 22 '13 at 15:16
  • 1
    ok now i get a NullReferenceException for the label in the property field...i uploaded my new code above. did i miss something here? – LeonidasFett Mar 22 '13 at 15:38
  • seems like u missed the initialization part that Tim has mentioned below. – Praveen Nambiar Mar 22 '13 at 15:48
  • hi dear friend this not work on update panel , how can i run this by update panel – R.Akhlaghi Jul 15 '17 at 05:15
  • you need to call Update method on your updatePanel. If you want to push your changes from the code behind to updatepanel directly. Did work for me. – afr0 Dec 10 '17 at 22:38
  • If you don't want to add the <%@ MasterType %> line to your aspx, it will also work if you cast the master page to its type in codebehind like so: `((YourMasterPageType)Master).ErrorMessage = "Message";` – AbeyMarquez Mar 23 '18 at 22:01
35

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";
11

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;
Diligent Key Presser
  • 4,183
  • 4
  • 26
  • 34
shweta
  • 111
  • 1
  • 3
4

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";
surya
  • 1,351
  • 1
  • 13
  • 29
3

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;
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

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")

Resource
  • 524
  • 4
  • 16
1

If you are trying to access an html element: this is an HTML Anchor...

My nav bar has items that are not list items (<li>) but rather html anchors (<a>)

See below: (This is the site master)

<nav class="mdl-navigation">
    <a class="mdl-navigation__link" href="" runat="server" id="liHome">Home</a>
    <a class="mdl-navigation__link" href="" runat="server" id="liDashboard">Dashboard</a>
</nav>

Now in your code behind for another page, for mine, it's the login page...

On PageLoad() define this:

HtmlAnchor lblMasterStatus = (HtmlAnchor)Master.FindControl("liHome");
lblMasterStatus.Visible =false;

HtmlAnchor lblMasterStatus1 = (HtmlAnchor)Master.FindControl("liDashboard");
lblMasterStatus1.Visible = false;

Now we have accessed the site masters controls, and have made them invisible on the login page.

Zoe
  • 27,060
  • 21
  • 118
  • 148
James Heffer
  • 686
  • 1
  • 6
  • 17