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

Introduction

In this article I will explain how to access or get master page controls from child or content page in asp.net

Description:

In previous posts I explained many articles relating to asp.net, gridview, SQL Server, JQuery, JavaScript and etc. Now I will explain how to access master page controls from child page or content page in asp.net.

To get master page control values in content 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">
<div>
MasterPage Label:
<asp:Label ID="lblMaster" runat="server" Text="Sample master Page label Control"/>
MasterPage Textbox:
<td><asp:textbox ID="txtMaster" runat="server" Text="Sample Master Page Textbox Control"/>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</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">
<b>Content Page Label Value:</b>
<asp:Label ID="lblContent" runat="server"/>
<br />
<b>Content Page Textbox Value:</b>
<asp:Textbox ID="txtContent" runat="server"/>
</asp:Content>
After completion of Default.aspx page add following namespaces in codebehind

C# Code


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 masterlbl = (Label)Master.FindControl("lblMaster");
TextBox mastertxt = (TextBox) Master.FindControl("txtMaster");
lblContent.Text = masterlbl.Text;
txtContent.Text = mastertxt.Text;
}
}
VB.NET Code


Imports System.Web.UI.WebControls

Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
Dim masterlbl As Label = DirectCast(Master.FindControl("lblMaster"), Label)
Dim mastertxt As TextBox = DirectCast(Master.FindControl("txtMaster"), TextBox)
lblContent.Text = masterlbl.Text
txtContent.Text = mastertxt.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

8 comments :

Anonymous said...

Hi,
I have a textbox,dropdownlist and button in master page.I want to get the selected item value of dropdownlist and text of textbox in a content page.How to do this? I was unable to do this in the manner suggested by you.
I tried of using public properties also,but didnot find solution.Can you help me? Thanks in advance.

My code in content page is

TextBox tt = (TextBox)Master.FindControl("TextBox1");
DropDownList ddl = (DropDownList)Master.FindControl("DropDownList1");
int x = Convert.ToInt16(ddl.SelectedItem.Value);
string text = tt.Text;

Anonymous said...

Is this methiod is called "Typecasting" ?

Narendran said...

Sir,

I need to fetch the asp.net menu control from master page to client page to change the menuitem text dynamically.
Please do the needful as soon possible.

Narendran N

Narendran said...

Hi Guys,

I found the result after a little tryout with errors.

Menu yourMenu = (Menu)Master.FindControl("menubar1");

MenuItem mitem = new MenuItem();

yourMenu.Items.Add(mitem);

mitem.Text = "Text for my menu";



Narendran N

Anonymous said...

can I use
masterlbl.Text = lblContent.Text;
instead of
lblContent.Text = masterlbl.Text;

I want to put value obtained in default.aspx in masterpage control.

antony said...

hi...
i have one menu control in master page, i am trying to access from content page for adding some more menu item...in asp.net 2.0
give me sample coding..



Kettavan said...

How to apply jquery in masterpage's content page....

Anonymous said...

Updating the master page when the control is within an update panel on the content page. i am trying lot but not get success ?

Give your Valuable Comments

Other Related Posts

© 2010-2012 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.