CODEDIGEST
Home » CodeDigest
Search
 

Technologies
 

Accessing Master Page Members from Content Pages in ASP.Net
Submitted By Satheesh Babu B
On 4/27/2009 8:39:22 AM
Tags: ASP.Net 2.0,CodeDigest  

Accessing Master Page Members from Content Pages in ASP.Net

 

At times, it is required to put some of the common methods, properties in master page and access it from the content pages.

This little article will help us to do that.

 

Pre-Condition

  •  The members of MasterPage that should be accessed in content pages should be public.  
  • Include a MasterType directive in every content page where we need to access MasterPage members.

<%@ MasterType virtualpath="~/MasterPage.master" %>

            This creates a strongly typed reference to the master page. If we are not using this directive, then we need to cast the Page.Master in Content page to the master page object and can directly access the master page members that are public.

 

Access or Call a method in Master Page from Content page

The below code snippet will help you to do that.

Master Page

public partial class MasterPage : System.Web.UI.MasterPage

{

    protected void Page_Load(object sender, EventArgs e)

    {

      

    }

    public string RemoveHTMLTags(string source)

    {

        string expn = "<.*?>";

        return Regex.Replace(source, expn, string.Empty);

    }

}

Content Page

  protected void Page_Load(object sender, EventArgs e)

    {       

        Response.Write(Master.RemoveHTMLTags("<b>Welcome to CodeDigest.Com!!!</b>"));            

    }

Note:

Include MasterType directive in the content page

 

Access a Master Page Property from Content Page

The below code snippet will help us in doing it.

Master Page

public partial class MasterPage : System.Web.UI.MasterPage

{

    private string _applicationName = "Test Sample";

 

    public string ApplicationName

    {

        get

        {

            return _applicationName;

        }

        set

        {

            _applicationName = value;

        }

    }

    protected void Page_Load(object sender, EventArgs e)

    {

      

    }   

}

 

Content Page

public partial class CallMasterPageFn : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

       Response.Write(Master.ApplicationName);      

      

    }

}

Note:

Include MasterType directive in the content page

 

Access a Control in Master Page from Content Page

Assume there is dropdownlist outside ContentPlaceHolder on the Master Page. Refer below,

 

<%@ 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:DropDownList ID="ddlThemes" runat="server">

                <asp:ListItem>Blue</asp:ListItem>

                <asp:ListItem>Green</asp:ListItem>

                <asp:ListItem>Red</asp:ListItem>

     </asp:DropDownList>

        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">

           </asp:contentplaceholder>

    </div>

    </form>

</body>

</html>

 

To Access the above dropdownlist(ddlThemes) from Content Page,

public partial class CallMasterPageFn : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

       DropDownList ddlTemp = Master.FindControl("ddlThemes") as DropDownList;

        if(ddlTemp != null)

       Response.Write(ddlTemp.SelectedValue);      

    }

}

Note:

Include MasterType directive in the content page

 

Accessing Members without using MasterType directive in Content page

As I said earlier, to Access without MasterType directive on the content page,

public partial class CallMasterPageFn : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

MasterPage master = Page.Master as MasterPage;

Response.Write(master.RemoveHTMLTags("<b>Welcome to CodeDigest.Com!!!</b>"));

Response.Write(master.ApplicationName);

DropDownList ddlTemp = master.FindControl("ddlThemes") as DropDownList;

        if(ddlTemp != null)

       Response.Write(ddlTemp.SelectedValue);

}

}

 

 

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..