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
- 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.
- 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.
- 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
After creating a new empty website project your solution explorer will look like this,
Property Basis Fetching Value from MasterPage
- Right click on Project in soluti
- On explorer add New Item MASTER PAGE named “MainMaster.master"
- Now Select MasterPage and give the name --> MainMaster.master
- 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.
Yes, In this dialog box you have to assign master page to the WebForm.
In this PropertyBaseWebForm.aspx page we will implement property base and fetch the control.
Select MainMaster.Master file insert following code just above ContentPlaceholder1,
- <span style="font-size:25px;background-color:greenyellow">
Value From Master Page
- <asp:Label ID="lblUserName" runat="server" Text="Ram Sharan"></asp:Label>
- </span>
- <br />
- <br />
Select MainMaster.Master and press F7 or select code behind file and type following property creation code.
- public string UserNamePropertyOnMasterPage
- {
- get
- {
-
- return lblUserName.Text;
- }
- set
- {
-
- lblUserName.Text = value;
- }
- }
(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,
- lblUserName.Font.Size = 27;
- lblUserName.BackColor = System.Drawing.Color.GreenYellow;
You have to add DIRECTIVE called “MasterType” in Select PropertyBaseWebForm.aspx and insert following code,
- <%@ MasterType VirtualPath ="~/MainMaster.master" %>
And Add the LABEL control to CONTENT2 placeholder,
- <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
- <asp:Label ID="lblCurrentUserName" runat="server" Text=""></asp:Label>
- </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
- <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MainMaster.master.cs" Inherits="MainMaster" %>
- <!DOCTYPE html>
-
- <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>
- <span style="font-size:25px;background-color:greenyellow">Value From Master Page:<asp:Label ID="lblUserName" runat="server" Text="Ram Sharan"></asp:Label></span>
- <br />
- <br />
- <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
-
- </asp:ContentPlaceHolder>
- </div>
- </form>
- </body>
- </html>
Code in MainMaster.master.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- public partial class MainMaster : System.Web.UI.MasterPage
- {
- public string UserNamePropertyOnMasterPage
- {
- get
- {
-
- return lblUserName.Text;
- }
- set
- {
-
- lblUserName.Text = value;
- }
- }
-
-
- protected void Page_Load(object sender, EventArgs e)
- {
- lblUserName.Font.Size = 27;
- lblUserName.BackColor = System.Drawing.Color.GreenYellow;
- }
- }
Code in PropertyBaseWebForm.aspx
- <%@ Page Title="" Language="C#" MasterPageFile="~/MainMaster.master" AutoEventWireup="true" CodeFile="PropertyBaseWebForm.aspx.cs" Inherits="PropertyBaseWebForm" %>
- <%@ MasterType VirtualPath ="~/MainMaster.master" %>
- <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content>
- <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
- <asp:Label ID="lblCurrentUserName" runat="server" Text=""></asp:Label>
- </asp:Content>
Code in PropertyBaseWebForm.aspx.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- public partial class PropertyBaseWebForm : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- lblCurrentUserName.Font.Size = 20;
- lblCurrentUserName.BackColor = System.Drawing.Color.Yellow;
-
- lblCurrentUserName.Text = "Value Received in Content Page : "+Master.UserNamePropertyOnMasterPage;
- }
- }
OUTPUT
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,
- MasterPage
- 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”
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.
Select FindControl.aspx and insert following code,
Add the LABEL control to CONTENT2 placeholder
- <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
- <asp:Label ID="lblFindControlUserName" runat="server" Text=""></asp:Label>
- </asp:Content>
Select FindControl.aspx.cs and insert following code in page load section,
-
- Label lbl = (Label)Page.Master.FindControl("lblUserName");
-
-
- lblFindControlUserName.Text = "Value Received in Content Page : "+lbl.Text;
Overall you require following codes for FINDCONTROL,
Code in FindControl.aspx
- <%@ Page Title="" Language="C#" MasterPageFile="~/MainMaster.master" AutoEventWireup="true" CodeFile="FindControl.aspx.cs" Inherits="FindControl" %>
- <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
- </asp:Content>
- <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
- <br />
- <br />
- <br />
- <br />
- <asp:Label ID="lblFindControlUserName" runat="server" Text=""></asp:Label>
- <br />
- <br />
- <span style="font-size:large">Above Content Page value coming from Master Page via FINDCONTROL() of MasterPage</span>
- </asp:Content>
Code in FindControl.aspx.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- public partial class FindControl : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
-
- Label lbl = (Label)Page.Master.FindControl("lblUserName");
-
-
- lblFindControlUserName.Text = "Value Received in Content Page : "+lbl.Text;
-
- lblFindControlUserName.Font.Size = 20;
- lblFindControlUserName.BackColor = System.Drawing.Color.CadetBlue;
- }
- }
OUTPUT