In this article, we shall learn how to access master page variable/property from the content page.
Introduction
Master page allows us to share the common content as well as layout to multiple pages. The best example is to show same header and footer and the same layout across different pages of the website. For example on ITFunda.com - an ASP.NET online training website you can notice that all pages have same header and footer but in between content is different. The layout in which header and footer exists is the master page (.master) and rest content comes from the content page (.aspx page)
In this article, let us learn how to access master page property or variable from the content pages.
Get hundreds of .NET Tips and Tricks and ASP.NET Online training here.
In some scenario, we need to access master page property or variable from the content pages, that can be done by
following this approach.
Below is my master page (MasterPage.master).
MASTER PAGE
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPageVariable.master.cs"
Inherits="NestedMasterPage_MasterPageVariable" %>
<!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>
<asp:Label ID="lblMasterLabel" runat="server" EnableViewState="false" />
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
MASTER PAGE CODE BEHIND
/// <summary>
/// Gets or sets the master page variable.
/// </summary>
/// <value>
/// The master page variable.
/// </value>
public string MasterPageVariable { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
lblMasterLabel.Text = MasterPageVariable;
}
Notice that we have a property declared in the the master page code behind (above)
ASPX PAGE
<%@ Page Title="Accessing Master page property" Language="C#" MasterPageFile="~/NestedMasterPage/MasterPageVariable.master"AutoEventWireup="true"
CodeFile="MasterPageVariablePage.aspx.cs"
Inherits="NestedMasterPage_MasterPageVariablePage" %>
<%@ MasterType VirtualPath="~/NestedMasterPage/MasterPageVariable.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>
In this content page, we have added virtual path of the Master page using MasterType directives.
ASPX PAGE CODE BEHIND
protected void Page_Load(object sender, EventArgs e)
{
Master.MasterPageVariable = "This text is written from Content page.";
}
In the above code snippet, we have a master page that has a asp:Label control. In the code behind of the master page we have a string type property named "MasterPageVariable". In the Page_Load of master page we have written this property value to the asp:Label control.
In the .aspx page, we have specified the MasterType directive to the master page that help us to access
the Matser page details (in this case Master page's “MasterPageVariable” property).
In the Page_Load event of the connet page, we have accessed the master page property “MasterPageVariable” using the Master object (Here because of the MasterType directives, we will have strongly typed reference of the Master page of this page) and set it’s value, this value will be set into the Master page label control becase of the code inside Page_Load event of the master page.
OUTPUT

When we run the page, our output looks something like above.
Here we have set the master page property value from the content page that is ultimately getting written on the page.
Hope this article will be useful. Thanks for reading and stay tuned for the next article in this series.
Do not forget to refer your friend about this website !