Monday, February 1, 2010

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




Please visit my new Web Site https://coderstechzone.com



In many forums i found that asp.net c# vb.net 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 asp.net 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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        
 <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">
        </asp:contentplaceholder>
    </div>
    </form>
</body>
</html>
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>

</asp:Content>
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.

1 comments:

Anonymous said...

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

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • 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