Access controls in Main Master page from content page in Nested Master Page
In this article we are going to see how to display the text of a TextBox in a child page. This TextBox is present in a First MasterPage . Here the scenario is we have a master page which is nested inside another master page and we have a webform whose masterpage has already defined parent master page.
In this article we are going to see how to display the text of a TextBox in a child page. This TextBox is present in a First MasterPage . Here the scenario is we have a master page which is nested inside another master page and we have a webform whose masterpage has already defined parent master page and we have to refer this textbox in the webform.
Below is the code of first master page:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage1.master.cs" Inherits="MasterPage1" %>
<!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></title>
<asp:ContentPlaceHolder id="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" ID="txtData" Text="MasterpageData"></asp:TextBox>
<asp:ContentPlaceHolder id="MainContent" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Then we have another master page which has parent master page as:MasterPage1.master. Below is the code in second master page"MasterPage.master"
<%@ Master Language="C#" MasterPageFile="~/MasterPage1.master" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:ContentPlaceHolder runat="server" ID="cph1"></asp:ContentPlaceHolder>
</asp:Content>
In the above master page we have a ContentPlaceHolder tag which can be used by child form to add its own controls.
Now add a webform which will have the above master page "MasterPage.master" as the Master page. Below is the code of the Default4.aspx page.
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cph1" Runat="Server">
<asp:Label runat="server" ID="lblMessage"></asp:Label>
</asp:Content>
In this page we have defined a Content tag whose ContentPlaceHolderID is "cph1".We can define our own controls in this Content tag. We have added a Label in this Content tag.
Now the requirement is to display the value of the textbox which is present in the MasterPage's Masterpage of this Default4.aspx page. Below is the code. In this code we have used the FindControl method of the Master.Master property of the current page to find the control reference of the first master page and display the text of the control. Below is the code:
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblMessage.Text = ((TextBox)Page.Master.Master.FindControl("txtData")).Text;
}
}