Let see how to access master page control,property,method from content page
Step 1
Create a web application and add the master page inside the web application.if you dont know how to create a master page then you have to see this Master Page article.
Step 2
After creating a master page add the lable control in the master page it is look like this
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %> <!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>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="lblUserName" runat="server"></asp:Label><br /> <asp:Label ID="lblFirstName" runat="server"></asp:Label><br /> <asp:Label ID="lblLastName" runat="server"></asp:Label><br /> <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> </div> </form> </body> </html>
Step 3
Write a method and property in a master page for accessing method and property from the content page.The members of MasterPage that should be accessed in content pages should be public,it is look like this
Master Page Code behind
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Xml.Linq; public partial class MasterPage : System.Web.UI.MasterPage { protected void Page_Load(object sender, EventArgs e) { try { lblFirstName.Text = FirstName; } catch (Exception) { } } #region Property /// <summary> /// Get and Set the first Name /// </summary> public String FirstName { get; set; } #endregion #region Method /// <summary> /// Set the last Name /// </summary> /// <param name="LastName"></param> public void LastName(String LastName) { try { lblLastName.Text = LastName.Trim(); } catch (Exception ex) { throw new Exception(ex.Message); } } #endregion }
Step 4
Add a content page it is look like this
Default.aspx
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> </asp:Content>
Step 5
Use @ MasterType directive in content page,it is look like this
<%@ MasterType VirtualPath="~/MasterPage.master"%>
@ MasterType
Include a MasterType directive in every content page where we need to access MasterPage members.
VirtualPath
Specifies the path to the file that generates the strong type.
finally the content page whould look like this
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %> <%@ MasterType VirtualPath="~/MasterPage.master"%> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> </asp:Content>Step 6
Access control,property and method from content page,it is look like this
Content Page Code behind [Default.aspx.cs]
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { try { /// Access Master Page control Label lblUserName = (Label)this.Master.FindControl("lblUserName"); lblUserName.Text = "KishorNaik"; //// Access Master Page Property this.Master.FirstName = "Kishor"; //// Access Master Page Method this.Master.LastName("Naik"); } catch (Exception) { } } }
Run the project
Download
Download Source Code
No comments:
Post a Comment