Read Master Page Controls in Content Page in ASP.NET

 

There are some situations where we have some requirement to read the Master page controls in Content Page for Asp.Net web application. We can read the Master page controls by using FindControl method of Master class in content page.

Here we discuss about how to read Master Page Label controls in content page by using Master class. As shown below we have master page with two label controls.

Site.Master

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="ReadMasterPageControlsAspNet.Site" %>

 

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

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

    <asp:ContentPlaceHolder ID="head" runat="server">

    </asp:ContentPlaceHolder>

</head>

<body>

    <form id="form1" runat="server">

    <div>

         <h1>Master Page Controls</h1>

         <asp:Label ID="lblMName" Text="Raj (from Master Page)" runat="server"></asp:Label> :

         <asp:Label ID="lblMCompany" Text="Microsoft (from Master Page)" runat="server"></asp:Label>

         <br /><br />        

        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">          

        </asp:ContentPlaceHolder>

    </div>

    </form>

</body>

</html>

 

 

We access these label controls of Master page in content page by using Master.FindControl method as shown below. We assign the master page controls values to the content page controls as shown below.

ChildPage.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="ChildPage.aspx.cs" Inherits="ReadMasterPageControlsAspNet.ChildPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <h1>Child Page Controls</h1>

     <asp:Label ID="lblCName" runat="server"></asp:Label> :

     <asp:Label ID="lblCCompany" runat="server"></asp:Label>

     <br /><br />

     <asp:Button ID="btnChild" Text="Read Master Page Controls" runat="server" OnClick="btnChild_Click" />

</asp:Content>

 

ChildPage.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

namespace ReadMasterPageControlsAspNet

{

    public partial class ChildPage : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

 

        protected void btnChild_Click(object sender, EventArgs e)

        {

            Label lblName = this.Master.FindControl("lblMName") as Label;

            Label lblCompany = this.Master.FindControl("lblMCompany") as Label;

 

            lblCName.Text = lblName.Text;

            lblCCompany.Text = lblCompany.Text;

        }

    }

}

 

Now run the application, you can find that master page control values are assigned to Content page by using FindControl() method of Master class.

                                                                                             ReadMasterPageControlsAspNet.zip (25.38 kb)