Monday, February 1, 2010

Access ASP.NET Master Page controls from Content Page and viceversa

Please visit my new Web Site

In many forums i found that c# developers ask expert how to get or read master page control from content page as well as how to get or read content page control from master page. Thats why in this tutorial i will explain how you can access or read content page controls from master page as well as access or read master page controls from content page.

If you want to read master page & content page controls using javascript then click here.

To do it by a simple example add a master page and a content page in your project.
The HTML Markup is given below:
Master Page:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="" >
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
 <asp:Label ID="lblMaster" runat="server" Text="Master Label"></asp:Label>
        <asp:TextBox ID="txtMaster" runat="server"></asp:TextBox>
        <br />
        <hr />
 <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
Content Page:
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="child_form.aspx.cs" Inherits="child_form" Title="Master & content page overview: " %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<asp:Label ID="lblChild" runat="server" Text="Child Label"></asp:Label>
<asp:TextBox ID="txtChild" runat="server"></asp:TextBox>

The server side code is given below:
Master Page:
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MasterPage : System.Web.UI.MasterPage
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            ContentPlaceHolder ContentPlaceHolder1 = (ContentPlaceHolder)this.FindControl("ContentPlaceHolder1");
            if (ContentPlaceHolder1 != null)
                Label lblChild = (Label)ContentPlaceHolder1.FindControl("lblChild");
                TextBox txtChild = (TextBox)ContentPlaceHolder1.FindControl("txtChild");
                if (lblChild != null)
                    lblChild.Text = "My Child Label";
                if (txtChild != null)
                    txtChild.Text = "My Child TextBox";
Content Page:
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class child_form : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            Label lblMaster=(Label)Master.FindControl("lblMaster");
            TextBox txtMaster = (TextBox)Master.FindControl("txtMaster");

            if (lblMaster != null)
                lblMaster.Text = "My Master Label";
            if (txtMaster != null)
                txtMaster.Text = "My Master TextBox";
If you look at my code then you will find that you can easily read master page controls from content page
by using Master.FindControl() method. And can read content page from master page by using this.FindControl method.

The output will be:
Master Content Overview

Hope now you can read master page controls & content page control from each other.


Anonymous said...

how to Access a control of masterpage in javascript which is in contentpage

Want to say something?

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash