How to access master page variable/property from the content page?

Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 46484 red flag

In this article, we shall learn how to access master page variable/property from the content page.


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 - 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 Language="C#" AutoEventWireup="true" CodeFile="MasterPageVariable.master.cs"

Inherits="NestedMasterPage_MasterPageVariable" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"



<html xmlns="">

<head runat="server">


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




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



<asp:Label ID="lblMasterLabel" runat="server" EnableViewState="false" />


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








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


<%@ Page Title="Accessing Master page property" Language="C#" MasterPageFile="~/NestedMasterPage/MasterPageVariable.master"AutoEventWireup="true"


Inherits="NestedMasterPage_MasterPageVariablePage" %>


<%@ MasterType VirtualPath="~/NestedMasterPage/MasterPageVariable.master" %>


<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">


<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">


In this content page, we have added virtual path of the Master page using MasterType directives.



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.


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 !


Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan

Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on | |

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)