Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

How to access asp.net content page controls from master page

Jun 20, 2012
Introduction

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.

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 RSS subscribe by email Subscribe by Email

6 comments :

Hafeez said...

This article (how to access child page controls from master page in asp.net.) helps for my project. Thanks

Unknown said...

how to access the child page grid view values from master page.......

Iti Tyagi said...

Will it be working when there are multiple content pages.

Unknown said...

how get controls from content page in master page , if we don't know the id of controls

Unknown said...

how to access controls from one content page to another content page?

Prabhat Kumar Mishra said...
This comment has been removed by the author.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.