SIGN UP MEMBER LOGIN:    
ARTICLE

Multiple Masters Pages in ASP.NET Application

Posted by Ashish Singhal Articles | ASP.NET Programming June 16, 2006
This article will explain that how can we use multiple master pages in our application and can access properties at run time.
Reader Level:

This article will show that how can we add multiple master pages in run time and can access their properties.

To achieve this, we need to add one Base master class which will have properties associated to master pages
and we can access properties associated to master pages on content pages im runtime.

// BASE Class:- Make a class inherited from System.Web.UI.Mster.MasterPage which will be placed in App_Code folder.

//BASE MASTER CLASS

public class BaseMaster : System.Web.UI.MasterPage

{

    string c = string.Empty;

    public virtual string Call

    {

        get

        {

            return "base";

        }

        set

        {

            c = value;

        }

    }

}

In above class we see one property call which declared as virtual property so we can override this property in other master pages inherited to this base master class and can access their properties associated to attached master at runtime.

First Master Page in our application

Below written master page class is using same call property using override as class is inherited from BaseMaster.

//HTML PAGE TAG
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage1.master.cs" Inherits="MasterPage1" %>

//CODEBEHIND
public
partial class MasterPage1 : BaseMaster

{

    string c = string.Empty;

    public override string Call

    {

        get

        {

            return "OnemoreChild";

        }

        set

        {

            c = value;

        }

    }

}

 

Second Master Page in our application

Below written master page class is using same call property using override as class is inherited from BaseMaster.

//HTML PAGE TAG
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage2.master.cs" Inherits="MasterPage2" %>

//CODEBEHIND
public
partial class MasterPage2 : BaseMaster

{

    string c = string.Empty;

    public override string Call

    {

        get

        {

            return "Child";

        }

        set

        {

            c = value;

        }

    }

} 

Now below written content page will show that how can we bind different master pages and can access their properties.

One important thing about master page : Runtime check master page and load all attached content pages on init event of page so if we need to bind in runtime so we have to bind on preinit event.

MASTERPAGEFILE property tells that what master page is being used inside content page so we need to change this property in runtime.

//CONTENT PAGE
//HTML TAG

<%@ Page Language="C#" MasterPageFile="~/MasterPage2.master"  AutoEventWireup="true" Title="Untitled Page" CodeFile="Default.aspx.cs" Inherits="Default"%>

<%@ MasterType TypeName="BaseMaster" %>

 

MasterType gives you master class code file from you can access your master page properties and functions.

//CODEBEHIND

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

{

    protected void page_preinit(object sender, EventArgs e)

    {

        if (Request.QueryString["Mode"] != null)

        {

            if(Request.QueryString["Mode"] == "one")

                MasterPageFile = "~/MasterPage1.master";

            else

                MasterPageFile = "~/MasterPage2.master";

        }

        this.Title = Master.Call;

    }      

}

Above code explains that how can we bind master page at runtime and can call property bound to that.

Login to add your contents and source code to this article
share this article :
post comment
 

Hello, Is possible integrate the same master page in different projects and solutions, I know the way to copy/paste the master page in each project, but my question is to centralize the master page in only one project, and use the same master page in different projects, so if I make a change in master page, I don’t need to make the same in each project.

Posted by Diego Sanchez Feb 06, 2008

How about if we were to use the masterpage from this different website , but pointing to the same content page - meaning the content page is available in another url address

Thanks

Posted by Mohd Ansari AbdulRahman Jul 01, 2006
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor