Get Master Page Control Value In Content Page

In this article you will come to know about how to fetch MasterPage control value in Content Page. Beside that you will get an answer to the following basic questions, 

  • What is Master Page?
  • What is Content Page?
  • Why we need the value from Master Page?
  • Ways to fetch the value from Master Page.

What is Master Page?

Asp.Net Master page help us to create consistent views for the pages. Master page behaves like container and parent page of Content page. In your application you can add numbers of Master pages depending upon your project requirement and layout. You can create your own contentplaceholder inside masterpage by default.For more detail about Master page working and behavior please refer following link,

https://msdn.microsoft.com/en-us/library/wtxbf3hh.aspx

What is Content Page?

Content page is one kind of child page of Master page because it renders inside master page. In-short, content page is associated with Master page. In the ASPX page you can see the link of master page. Without link of master page content page is not attached with Master page.

For more detail about content page workings and behavior please refer to the following link,

https://msdn.microsoft.com/en-us/library/fft2ye18.aspx

Why we need the value from Master Page?

On many occasions we need value of control which resides under master page. Master page and Content page work together to achieve the complete goal of web application. Master page is container of Content page and content page displays the content under master page container. There are two ways which I like to pull / fetch the control value of master page from content page.

Ways to fetch the value from Master Page

  1. Property base
    In property base work we create a property in masterpage and in content page we define MasterType directive defining VirtualPath. Afterwards your content is ready to retrieve the property of master page.

    Example
    Master.Propertyname

    Useful in Scenario
    To get current user name, user role  etc..  from master page.
  1. FindControl
    In FindControl() method is also best way to fetch the masterpage control to content page. FinControl() is by default inside method of masterpage.

Example

Label control named lblUserName on master page.

  1. Label lblUserVal = (Label)Page.Master.FindControl("lblUserName");  

Step By Step Implementation 

  • To achieve this we have to create a one Empty Web Site Project call MasterToContent

    ASP.NET

    After creating a new empty website project your solution explorer will look like this,

    ASP.NET

Property Basis Fetching Value from MasterPage

  • Right click on Project in soluti
  • On explorer add New Item MASTER PAGE named “MainMaster.master"

    ASP.NET
  • Now Select MasterPage and give the name --> MainMaster.master

    ASP.NET
  • Again right click on Project in solution explorer and Add New Item Web Form PAGE named “PropertyBaseWebForm.aspx” and Tick Check Box Select Master Page. By ticking check box webform becomes content page of selected master page. You will select your master page after pressing ADD button.

    ASP.NET

Yes, In this dialog box you have to assign master page to the WebForm.

ASP.NET

In this PropertyBaseWebForm.aspx page we will implement property base and fetch the control.

Select MainMaster.Master file insert following code just above ContentPlaceholder1,

  1. <span style="font-size:25px;background-color:greenyellow" 

Value From Master Page

  1. <asp:Label ID="lblUserName" runat="server" Text="Ram Sharan"></asp:Label>  
  2. </span>  
  3. <br />  
  4. <br />  

Select MainMaster.Master and press F7 or select code behind file and type following property creation code.

  1. public string UserNamePropertyOnMasterPage  
  2.     {  
  3.         get  
  4.         {  
  5.             // Get value of control on master page  
  6.             return lblUserName.Text;  
  7.         }  
  8.         set  
  9.         {  
  10.             // Set new value for control on master page  
  11.             lblUserName.Text = value;  
  12.         }  
  13.     }  

(In above code one property is created named “UserNamePropertyOnMasterPage” with GET we will receive the lblUserName.Text value . With SET we will assign the value to lblUserName.Text)

Inside PageLoad event of MainMaster.Master insert the following code,

  1. lblUserName.Font.Size = 27;  
  2. lblUserName.BackColor = System.Drawing.Color.GreenYellow;  

You have to add DIRECTIVE called “MasterType” in Select PropertyBaseWebForm.aspx and insert following code,

  1. <%@ MasterType VirtualPath ="~/MainMaster.master" %>  

And Add the LABEL control to CONTENT2 placeholder,

  1. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">  
  2.     <asp:Label ID="lblCurrentUserName" runat="server" Text=""></asp:Label>  
  3. </asp:Content>  

For more information on DIRECTIVES please visit this link,

https://msdn.microsoft.com/en-us/library/t8syafc7.aspx

Overall you require the following codes,

Code in MainMaster.master

  1. <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MainMaster.master.cs" Inherits="MainMaster" %>  
  2. <!DOCTYPE html>  
  3.   
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title></title>  
  7.     <asp:ContentPlaceHolder id="head" runat="server">  
  8.     </asp:ContentPlaceHolder>  
  9. </head>  
  10. <body>  
  11.     <form id="form1" runat="server">  
  12.     <div>  
  13.         <span style="font-size:25px;background-color:greenyellow">Value From Master Page:<asp:Label ID="lblUserName" runat="server" Text="Ram Sharan"></asp:Label></span>  
  14.         <br />  
  15.         <br />  
  16.         <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">  
  17.           
  18.         </asp:ContentPlaceHolder>  
  19.     </div>  
  20.     </form>  
  21. </body>  
  22. </html>  

Code in MainMaster.master.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. public partial class MainMaster : System.Web.UI.MasterPage  
  9. {  
  10.     public string UserNamePropertyOnMasterPage  
  11.     {  
  12.         get  
  13.         {  
  14.             // Get value of control on master page  
  15.             return lblUserName.Text;  
  16.         }  
  17.         set  
  18.         {  
  19.             // Set new value for control on master page  
  20.             lblUserName.Text = value;  
  21.         }  
  22.     }  
  23.   
  24.   
  25. protected void Page_Load(object sender, EventArgs e)  
  26.     {  
  27.         lblUserName.Font.Size = 27;  
  28.         lblUserName.BackColor = System.Drawing.Color.GreenYellow;  
  29.     }  
  30. }  

Code in PropertyBaseWebForm.aspx

  1. <%@ Page Title="" Language="C#" MasterPageFile="~/MainMaster.master" AutoEventWireup="true" CodeFile="PropertyBaseWebForm.aspx.cs" Inherits="PropertyBaseWebForm" %>  
  2.     <%@ MasterType VirtualPath ="~/MainMaster.master" %>  
  3.         <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content>  
  4.         <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">  
  5.             <asp:Label ID="lblCurrentUserName" runat="server" Text=""></asp:Label>  
  6.         </asp:Content>  

Code in PropertyBaseWebForm.aspx.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. public partial class PropertyBaseWebForm : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.         lblCurrentUserName.Font.Size = 20;  
  13.         lblCurrentUserName.BackColor = System.Drawing.Color.Yellow;  
  14.   
  15.         lblCurrentUserName.Text = "Value Received in Content Page : "+Master.UserNamePropertyOnMasterPage;  
  16.     }  
  17. }  

OUTPUT

ASP.NET

FindControl Method Basis Fetching Value from MasterPage

Now we are going to implement step by step FINDCONTROL basis fetching value from masterpage to content page. In this process we require the following things,

  1. MasterPage
  2. WebForm

As per our previous sample we already created a masterpage; that's why there is no need to create again but we have to create a new webform which is good to understood. 

So let's start….

  • Right click on Project in solution explorer and Add New Item WEBFORM PAGE named “FindControl.aspx”

    ASP.NET

Tick Check Box Select Master Page

By ticking check box webform becomes content page of selected master page. You will select your master page after pressing ADD button.

ASP.NET

Select FindControl.aspx and insert following code,

Add the LABEL control to CONTENT2 placeholder 

  1. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">  
  2.    <asp:Label ID="lblFindControlUserName" runat="server" Text=""></asp:Label>  
  3. </asp:Content>  

Select FindControl.aspx.cs and insert following code in page load section,

  1.    //Define Label Control and Fetch the control of MASTER PAGE  
  2.    Label lbl = (Label)Page.Master.FindControl("lblUserName");  
  3.   
  4. //Set the value to CONTENT PAGE label control.  
  5.     lblFindControlUserName.Text = "Value Received in Content Page : "+lbl.Text;  

Overall you require following codes for FINDCONTROL, 

Code in FindControl.aspx 

  1. <%@ Page Title="" Language="C#" MasterPageFile="~/MainMaster.master" AutoEventWireup="true" CodeFile="FindControl.aspx.cs" Inherits="FindControl" %>  
  2. <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">  
  3. </asp:Content>  
  4. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">  
  5.     <br />  
  6.     <br />  
  7.     <br />  
  8.     <br />  
  9.     <asp:Label ID="lblFindControlUserName" runat="server" Text=""></asp:Label>  
  10.     <br />  
  11.     <br />  
  12.     <span style="font-size:large">Above Content Page value coming from Master Page via FINDCONTROL() of MasterPage</span>  
  13. </asp:Content>  

Code in FindControl.aspx.cs 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. public partial class FindControl : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.         //Define Label Control and Fetch the control of MASTER PAGE  
  13.         Label lbl = (Label)Page.Master.FindControl("lblUserName");  
  14.   
  15.         //Set the value to CONTENT PAGE label control.  
  16.         lblFindControlUserName.Text = "Value Received in Content Page : "+lbl.Text;  
  17.   
  18.         lblFindControlUserName.Font.Size = 20;  
  19.         lblFindControlUserName.BackColor = System.Drawing.Color.CadetBlue;  
  20.     }  
  21. }  

OUTPUT

ASP.NET

Up Next
    Ebook Download
    View all
    Learn
    View all