In this article, we are going to learn how to access master page control in content page (child page) in asp.net.
1) Access master page textbox's text from content page.
Step 1: Create a master page with name MasterPage.Master.
Step 2: Add a Textbox control named as txt1MasterPage in master page. Below is the master page designer code.
Inherits="WebApplication1.MasterPage" %>
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">
This TextBox is in master page:
asp:TextBox ID="txt1MasterPage" runat="server"> asp:TextBox>
asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
asp:ContentPlaceHolder>
form>
body>
html>
Step 3: Add a content page with name WebForm1.aspx.
Step 4: Create a textbox control and a button control in content page. Below is the content page designer code.
Inherits="WebApplication1.WebForm1" MasterPageFile="~/MasterPage.Master" %>
asp:Content ID="MyContent" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
hr />This is inside child page:br />
asp:TextBox ID="txt1Contentpage" runat="server">asp:TextBox>
asp:Button ID="btn1Contentpage" runat="server"
Text="Get Content of TextBox of Master page" OnClick="btn1Contentpage_Click" />
asp:Content>
Step 5: Below is the content page code behind file.
protected void btn1Contentpage_Click(object sender, EventArgs e)
{
TextBox txt1MasterPage = (TextBox)Page.Master.FindControl("txt1MasterPage");
txt1Contentpage.Text = txt1MasterPage.Text;
}
Output:
Output will be shown as below.
2) Set master page textbox's text from content page.
Step 1: Create a master page with name Site1.Master.
Step 2: Add a textbox control named as txt2MasterPage in master page. Below is the master page designer code.
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">
This is inside master page:
asp:TextBox ID="txt2MasterPage" runat="server">
asp:TextBox>
asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
asp:ContentPlaceHolder>
form>
body>
html>
Step 3: Add a content page with name WebForm1.aspx.
Step 4: Create a textbox control and a button control in content page. Below is the content page designer code.
Inherits="WebApplication1.WebForm1" MasterPageFile="~/Site1.Master" %>
asp:Content ID="ChildContentID1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
hr />This is inside child page:
asp:TextBox ID="txt2Contentpage" runat="server">asp:TextBox>
br />
asp:Button ID="btn2Contentpage" runat="server"
Text="Set Master Page Textbox's text from Content Page" OnClick="btn2Contentpage_Click" />
asp:Content>
Step 5: Below is the content page code behind file.
protected void btn2Contentpage_Click(object sender, EventArgs e)
{
TextBox txt2MasterPage = (TextBox)Page.Master.FindControl("txt2MasterPage");
txt2MasterPage.Text = txt2Contentpage.Text;
}
Output:
Output will be shown as below.