How to access Master page control from the content page?

Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 32204 red flag
Rating: 3 out of 5  
 1 vote(s)

In this article, we shall learn how to access Master page control 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 control from the content page. To demonstrate this I have created a demo page.

Get hundreds of .NET Tips and Tricks and ASP.NET Online training here.

In some scenario, we need to access master page controls from the content page, that can be done by
following this approach.

Below is my master page (MasterPage.master).


<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="NestedMasterPage_MasterPage" %>


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



<html xmlns="">

<head runat="server">


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




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


<p style="color: Red;">This text is from parent master page</p>

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



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







<%@ Page Title="" Language="C#" MasterPageFile="~/NestedMasterPage/MasterPage.master"

AutoEventWireup="true" CodeFile="AccessMasterPageLabel.aspx.cs" Inherits="NestedMasterPage_AccessMasterPageLabel" %>


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


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




protected void Page_Load(object sender, EventArgs e)


Label lblMaster = (Label)this.Master.FindControl("lblMasterMessage");

if (lblMaster != null)


lblMaster.Text = "This text is written from the content page.";




In the above code snippet, we have an asp:Label control on the master page. From the code behind of the content page, we are trying to access the Master page using this.Master.FindControl method. It’s ideal to check for the null value for that variable (to ensure that the control is found successfully) and then we have specified a string for that label text.


As displayed in the above image, you can see that text specified to the Label control from the content page is appearing on the output page.

Hope this article will be useful. Keep reading!

Stay tuned for the  next article!

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)