Introduction:
In this article I will explain how to access or get master page controls from child or content page in asp.net
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.
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. |
|||
|
|||
13 comments :
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;
Is this methiod is called "Typecasting" ?
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
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
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.
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..
How to apply jquery in masterpage's content page....
Updating the master page when the control is within an update panel on the content page. i am trying lot but not get success ?
Hello sir,
I want to call a method by an anchor tag and the anchor tag is written at aspx.cs file with innerhtml.
For exapmle--
DivId.InnerHtml=InnerHtml+ "< a href='#' runat='server' > Clickme < /a >";
This code is written at aspx.cs file.
Please guide me how to call a server side method by this anchor tag.
I Need to fetch the value from child page to master page while child page loading , is it possible to do so?????
Javascript pop running that text box value how to get in server side
Note: Only a member of this blog may post a comment.