Change Master Page dynamically based on User Selection in ASP.NET

 

In Asp.Net we can change master page dynamically at runtime in Page_PreInit() event. In our web application we can show different master pages based on User selection, so once user selected particular master page on next login it shows selected master page only, it is possible if we store the selection in Profile object. In this article we discuss about how to change master page dynamically based on user selection.

 

Open Visual Studio => Create New Asp.Net Web Application => Add two master pages FirstMaster.Master & SecondMaster.Master as shown below.

 

FirstMaster.Master

 

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="FirstMaster.master.cs"

    Inherits="ASPNETDynamicMasterPage.FirstMaster" %>

 

<!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></title>

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

    </asp:ContentPlaceHolder>

</head>

<body>

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

    <div>

        <h1>

            This is First Master Page</h1>

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

        </asp:ContentPlaceHolder>

    </div>

    </form>

</body>

</html>

 

SecondMaster.Master

 

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="SecondMaster.master.cs"

    Inherits="ASPNETDynamicMasterPage.SecondMaster" %>

 

<!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></title>

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

    </asp:ContentPlaceHolder>

</head>

<body>

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

    <div>

        <h1>

            This is Second Master Page</h1>

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

        </asp:ContentPlaceHolder>

    </div>

    </form>

</body>

</html>

 

Now create content page as shown below, here we set the MasterPage as FirstMaster by default.

 

default.aspx

 

<%@ Page Title="" Language="C#" MasterPageFile="~/FirstMaster.Master" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="ASPNETDynamicMasterPage._default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">   

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

This is Content Page<br /><br />

<asp:DropDownList ID="ddlMaster" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlMaster_SelectedIndexChange">

    <asp:ListItem Value="FirstMaster.Master">FirstMaster</asp:ListItem>

    <asp:ListItem Value="SecondMaster.Master">SecondMaster</asp:ListItem>

</asp:DropDownList>

</asp:Content>

 

Add profile object to Web.config file to store user selection as shown below.

 

Web.config

 

<?xmlversion="1.0"?>

<configuration>

  <system.web>

    <profile>

      <properties>

        <addname="MasterPageFile"defaultValue="FirstMaster.master"/>

      </properties>

    </profile>

    ---------------------------

    -----------------------------

</configuration>


Change the content page master page by user selection in Page_PreInit() event as shown below.

 

using System;

 

namespace ASPNETDynamicMasterPage

{

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

    {

        string sMasterPage = string.Empty;

        protected void Page_PreInit(object sender, EventArgs e)

        {

            MasterPageFile = Context.Profile["MasterPageFile"].ToString();

            sMasterPage = MasterPageFile;

        }

 

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                ddlMaster.SelectedValue = sMasterPage;

            }

        }

 

        protected void ddlMaster_SelectedIndexChange(object sender, EventArgs e)

        {

            Context.Profile["MasterPageFile"] = ddlMaster.SelectedValue;

            Response.Redirect("default.aspx");

        }

    }

}

 

As shown above we are storing the user selection in the Profile object MasterPageFile property on ddlMaster_SelectedIndexChange event.

 

Run the application, the output is shown below.

 

 

 

By default content page is using FirstMaster.Master page. Select SecondMaster from drop down list, the output is as shown below.

 

 

 

On next open, the content page is display with SecondMaster.Master master page only, because we store the user selection in Profile object and set the content master page from Profile object in Page_PreInit() event.

 

                                                                             ASPNETDynamicMasterPage.zip (204.22 kb)