Multiple Masters Pages in ASP.NET Application


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.


Similar Articles