Loading Master Pages dynamically in ASP.NET

 

Sometimes you have requirement to change master page dynamically for content pages in your project. By doing this you can enable the users of your website to customize the appearance of the website by loading different Master Pages for different users.

 

Generally your content page is merged with your master page before page load event in Asp.Net Page Life Cycle. So, you cannot change the master page in Page_Load event. You can change the master page for your content page in Page_PreInit event. In Page_PreInit event you can change master page by assigning the master page name to the MasterPageFile property of your content page.

 

For example you have two master pages MasterPage1.master, MasterPage2.master and one content page Default.aspx as shown below.

 

MasterPage1.master:

 

<%@ Master Language="C#" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<script runat="server">

</script>

 

<html xmlns="http://www.w3.org/1999/xhtml">

 

<head runat="server">

 

    <title>Master Page 1</title>

 

    <asp:ContentPlaceHolder id="head" runat="server">

 

    </asp:ContentPlaceHolder>

 

</head>

 

<body>

 

    <form id="form1" runat="server">

 

    <div>

 

         <h1>This is Master Page 1</h1>

 

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

 

       

 

        </asp:ContentPlaceHolder>

 

    </div>

 

    </form>

 

</body>

 

</html>

 

 

MasterPage2.master:

 

<%@ Master Language="C#" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<script runat="server">

</script>

 

<html xmlns="http://www.w3.org/1999/xhtml">

 

<head runat="server">

 

    <title>Master Page 2</title>

 

    <asp:ContentPlaceHolder id="head" runat="server">

 

    </asp:ContentPlaceHolder>

 

</head>

 

<body>

 

    <form id="form1" runat="server">

 

    <div>

 

        <h1>This is Master Page 2</h1>

 

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

 

         

 

        </asp:ContentPlaceHolder>

 

    </div>

 

    </form>

 

</body>

 

</html>

 

 

Default.aspx:

 

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

 

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

</asp:Content>

 

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

 

Change Master Page:

 

<ul>

 

<li>

 

<a href="Default.aspx?masterpage=MasterPage1">For Master Page 1</a>

 

</li>

 

<li>

 

<a href="Default.aspx?masterpage=MasterPage2">For Master Page 2</a>

 

</li>

 

</ul>

 

</asp:Content>

 

As shown above default master page for Default.aspx page is MasterPage1.master and in Default.aspx page we provided the links to change the master page. Now based on the Querystring parameter masterpage value change the master page of your content page Default.aspx in Page_PreInit event as shown below.

 

using System;

 

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

{

    protected void Page_Load(object sender, EventArgs e)

    {

    }

 

    protected void Page_PreInit(object sender, EventArgs e)

    {

        if (Request["masterpage"] != null)

        {

            if (Request.QueryString["masterpage"] == "MasterPage1")

                this.MasterPageFile = "MasterPage1.master";

            else if (Request.QueryString["masterpage"] == "MasterPage2")

                this.MasterPageFile = "MasterPage2.master";         

        } 

    }

}

 

Now run your application, if you click For Master Page 1 link the output is as shown below.

 

 

and if you click For Master Page 2 link the output is as shown below.

 

       

                   

                                                                                          LoadMasterPagesDynamically.zip (3.87 kb)