Custom Search Tailored for Microsoft .NET  
Aspexception.com Skip Navigation Links
Error RepositoryExpand Error Repository
.NET ResourcesExpand .NET Resources
About
Skip Navigation LinksHome > .NET Resources > How to tips... > Use Master Page Contents
Reference ASP.NET Master Page Content
You can write code in content pages that references public properties and public methods of the master pages.
You can also write code in content pages that references controls on the master pages.

To reference a public member on the master page

First you need to declare a public member in the master page. For instance,
public string CompanyName;
Now you can reference the public member of the master page in content page,
CompanyName.Text = Master.CompanyName;

To reference a control on the master page: Master.FindControl

Use Master.FindControl to get the control objects in master page. In the following example, in MasterPage.master, there is one label server control masterPageLabel. You can use Master.FindControl("masterPageLabel") to get the object of this control.
As for the TextBox1 control, because the TextBox control is inside a ContentPlaceHolder control, you must first get a reference to the ContentPlaceHolder and then use its FindControl method to locate the TextBox control :
MasterPage.master
<%@ Master Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>My Master Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        </asp:contentplaceholder>
        <br />
        <asp:Label ID="masterPageLabel" runat="server" Text="Master Page Label"></asp:Label>
    </div>
    </form>
</body>
</html>
Content page: test_mastercontent.aspx
<%@ Page Language="C#" MasterPageFile="~/MasterPage3.master" Title="Master Page" %>
<script runat="server">
void Page_Load()
{
    // Gets a reference to a TextBox control inside 
    // a ContentPlaceHolder
    ContentPlaceHolder objContentPlaceHolder;
    TextBox objTextBox;
    objContentPlaceHolder = 
      (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
    if(objContentPlaceHolder != null)
    {
        objTextBox = 
            (TextBox) objContentPlaceHolder.FindControl("TextBox1");
        if(objTextBox != null)
        {
            objTextBox.Text = "TextBox client ID: " + objTextBox.ClientID;
        }
    }
    
    // Gets a reference to a Label control that not in 
    // a ContentPlaceHolder
    Label objLabel = (Label) Master.FindControl("masterPageLabel");
    if(objLabel != null)
    {
        Label1.Text = "Content Page Label = " + objLabel.Text;
    }
}
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
            <asp:TextBox ID="TextBox1" runat="server" Width="400px"></asp:TextBox>
            <br />
            <asp:Label ID="Label1" runat="server" />
</asp:Content>
Site Map  Copyright © Aspexception.com 2008. All rights Reserved. Terms of Use