Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Change Master Page @ Run-time

0.00/5 (No votes)
6 Feb 2015 1  
This tip will give complete knowledge of how to change master page, render controls and accessing its eventhandlers @ runtime.

Introduction

This tip is for those who are looking for basic/advanced knowledge about how to change Master page @runtime and how to render controls @ runtime. This will allow user to save his/her personal settings just like in gmail.

Using the Code

Just download the attached zip file and extract it. This application is developed using ASP.NET 4.0 framework.

Solution explorer view is as below for your reference.

Create a Base class for all Master pages named "BaseMaster". This will handle Page_Load event of all Master Pages.

C#
using System.Web.UI.WebControls;
 
public class BaseMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.RawUrl == "/Settings.aspx")
            {
                DropDownList drop = new DropDownList();
                drop.ID = "DropDownThemes";
                drop.Items.Add(new ListItem("Site1", "Site1.Master"));
                drop.Items.Add(new ListItem("Site2", "Site2.Master"));
                drop.Items.Add(new ListItem("Site3", "Site3.Master"));
                drop.Enabled = true;
                drop.AutoPostBack = true;
                drop.SelectedIndexChanged += new EventHandler(this.drop_SelectedIndexChanged);
                if (Session["MasterPage"] != null)
                {
                    drop.SelectedValue = Session["MasterPage"].ToString();
                }
                try
                {
                    ((Site1)this).dropDownTheme = drop;
                }
                catch
                {
                    try
                    {
                        ((Site2)this).dropDownTheme = drop;
                    }
                    catch
                    {
                        ((Site3)this).dropDownTheme = drop;
                    }
                }
            }
        }

        protected void drop_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Session["MasterPage"] = ((Site1)this).dropDownTheme.SelectedValue.ToString();
            }
            catch
            {
                try
                {
                    Session["MasterPage"] = ((Site2)this).dropDownTheme.SelectedValue.ToString();
                }
                catch
                {
                    Session["MasterPage"] = ((Site3)this).dropDownTheme.SelectedValue.ToString();
                }
            }

            Response.Redirect(Request.Url.AbsoluteUri);
        }
    }

Here, I am creating a DropDownList control and assigning to MasterPage. Eventhandler "SelectedIndexChanged" is assigned to the same.

Inherit this BaseMaster class to all Master pages, say Site1.Master, Site2.Master, Site3.Master.

C#
public partial class Site1 : BaseMaster
    {
        
        public DropDownList dropDownTheme
        {
            get { return (DropDownList)this.PlaceHolder1.FindControl("DropDownThemes"); }
            set { PlaceHolder1.Controls.Add((DropDownList)value); }
        }
    } 

Notice that I added a Public property "dropDownTheme". This property is created because here I am accessing the dropdown control from "BaseMaster" class not in same class "Site1".

HTML of Site1 is here. Same HTML is used in remaining Master page also (Site2.Master and Site3.Master).

HTML
<%@ Master Language="C#" AutoEventWireup="true" 
CodeBehind="Site1.master.cs" Inherits="WebApplication4.MasterPages.Site1" %>

<!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 style="background-color: Aqua">
    <form id="form1" runat="server">
    This is Site 1 Master page
    <table>
        <tr>
            <td style="text-align: left">
                <h1>
                    This is Header</h1>
            </td>
            <td style="text-align: right; color: Red">
                <a href="../../Settings.aspx" 
                id="LinkToSettings">Settings</a>
            </td>
        </tr>
        <tr>
            <td>
                This is Menu section from Master<br />
                <asp:PlaceHolder ID="PlaceHolder1" 
                runat="server"></asp:PlaceHolder>
            </td>
            <td>
                <fieldset>
                    <legend>Content Page</legend>
                    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" 
                    runat="server">
                    </asp:ContentPlaceHolder>
                </fieldset>
            </td>
        </tr>
        <tr style="text-align: center">
            <td colspan="2">
                <small>All Rights Reserved with no one.</small>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

Create a content page "Settings.aspx" HTML is below:

HTML
<%@ Page Title="" Language="C#" 
MasterPageFile="~/MasterPages/Site1.Master" AutoEventWireup="true" 
CodeBehind="Settings.aspx.cs" Inherits="WebApplication4.Settings" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">

</asp:Content>
<asp:Content ID="Content2" 
ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    This is Settings Page with Master<br>
    <asp:Button ID="btnBack" runat="server" Text="Back" 
    onclick="btnBack_Click" />
    <asp:Button ID="btnReset" runat="server" Text="Reset" 
    onclick="btnReset_Click" />
</asp:Content>

In the code behind page, add the below events:

C#
public partial class Settings : BasePage
    {
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("HomePage.aspx");
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            Session["MasterPage"] = "Site1.Master" ;
            Response.Redirect(Request.Url.AbsoluteUri);
        }        
    }

Here, this drop down will render only on Settings.aspx page. If there are any other content pages, dopdown will not be shown as only settings page is designed to allow so.

Create a Class file and write the below code to choose Master page.

C#
public class BasePage : System.Web.UI.Page
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (Session["MasterPage"] != null)
            {
                MasterPageFile = WebConfigurationManager.AppSettings
                ["MasterFileFolder"] + Session["MasterPage"].ToString();
            }
        }
    }

All the webpages which will use Master page have to inherit this class. This will handle Page_preInt event for all pages.

Add a master page folder path to .config file:

XML
<appSettings>
    <add key="MasterFileFolder" value="~\MasterPages\"/>
  </appSettings>

Enjoy the code. :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here