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.
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
.
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
).
<%@ 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>