Introduction: 
In this article I will explain how to access or get content or child page controls from master page in asp.net.
 
 
 
 
 
 
In this article I will explain how to access or get content or child page controls from master page in asp.net.
Description: 
In previous post I explained how to access master page controls from content page in asp.net. Now I will explain how to access content page controls from master page in asp.net.
In previous post I explained how to access master page controls from content page in asp.net. Now I will explain how to access content page controls from master page in asp.net.
To
get child or content page control values in master page first write the
following code in master page like this 
MasterPage.Master
| 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<title>Master Page with Controls</title> 
<asp:ContentPlaceHolder id="head" runat="server"> 
</asp:ContentPlaceHolder> 
</head> 
<body> 
<form id="form1"
  runat="server"> 
MasterPage Label: 
<asp:Label ID="lblMaster"
  runat="server"
  /> 
MasterPage Textbox: 
<asp:textbox ID="txtMaster"
  runat="server"
  /> 
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server"> 
</asp:ContentPlaceHolder> 
</form> 
</body> 
</html> | 
After
that write the code in Content Page Default.aspx
will be like this
| 
<%@ Page Language="C#"
  AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default"
  MasterPageFile="~/MasterPage.master"
  %> 
<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> 
<fieldset id="fld"> 
<legend><b>Content Page</b></legend> 
Content Page Label Value: 
<asp:Label ID="lblContent"
  Text="Sample
  Content Page label Control" runat="server"/>  
<br /> 
Content Page Textbox Value: 
<asp:Textbox ID="txtContent"
  Text="Sample
  Content Page Textbox Control" runat="server"/>  
</fieldset> 
</asp:Content> | 
After
that add following namespaces in MasterPage.master.cs
page in codebehind
C#
Code (MasterPage)
| 
using System; 
using System.Web.UI.WebControls; | 
Now
add following code in code behind
| 
protected void
  Page_Load(object sender, EventArgs e) 
{ 
if (!IsPostBack) 
{ 
Label cotentlbl = (Label)ContentPlaceHolder1.FindControl("lblContent"); 
TextBox contenttxt = (TextBox)ContentPlaceHolder1.FindControl("txtContent"); 
lblMaster.Text = cotentlbl.Text; 
txtMaster.Text = contenttxt.Text; 
} 
} | 
VB.NET
Code
(MasterPage)
| 
Imports System.Web.UI.WebControls 
Partial Public Class MasterPage 
Inherits System.Web.UI.MasterPage 
Protected Sub
  Page_Load(ByVal sender As Object, ByVal e As
  EventArgs) 
If Not IsPostBack Then 
Dim cotentlbl As Label = DirectCast(ContentPlaceHolder1.FindControl("lblContent"), Label) 
Dim contenttxt As TextBox = DirectCast(ContentPlaceHolder1.FindControl("txtContent"), TextBox) 
lblMaster.Text = cotentlbl.Text 
txtMaster.Text = contenttxt.Text 
End If 
End Sub 
End Class | 
Demo
| If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. | |||
|  Subscribe by RSS  Subscribe by Email | |||

 
6 comments :
This article (how to access child page controls from master page in asp.net.) helps for my project. Thanks
how to access the child page grid view values from master page.......
Will it be working when there are multiple content pages.
how get controls from content page in master page , if we don't know the id of controls
how to access controls from one content page to another content page?
Note: Only a member of this blog may post a comment.