In this article I will explain how to access or get master page controls from child 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.
<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>
|
<%@ 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>
|
using System;
using System.Web.UI.WebControls;
|
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;
}
}
|
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
|
|
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
|
|||
|
|

Subscribe by RSS
Subscribe by Email
2 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" ?