Loading Master Pages Dynamically in ASP.NET 3.5
Posted by Tihomir Ivanov on 16 October 2008 04:48
Rating: 0.00
You can associate different Master Pages dynamically with a content page. This is useful in
two situations.
First, you can enable the users of your website to customize the appearance of the website
by loading different Master Pages. You can display a menu of Master Pages, and allow your
users to pick their favorite layout.
Another situation in which loading Master Pages dynamically is useful concerns co-branding.
Imagine that your company needs to make its website look like a partner website.
When users link to your website from the partner website, you don’t want users to know
that they are traveling to a new website. You can maintain this illusion by dynamically
loading different Master Pages based on a query string passed from a partner website.
A Master Page is merged with a content page very early in the page execution life-cycle.
This means that you cannot dynamically load a Master Page during the Page Load event.
The only event during which you can load a Master Page is during the Page PreInit
event. This is the first event that is raised during the page execution life cycle.
<%@ Page Language=”C#” MasterPageFile=”~/Dynamic1.master” %>
<script runat=”server”>
protected void Page_PreInit(object sender, EventArgs e)
{
if (Request[“master”] != null)
{
switch (Request[“master”])
{
case “Dynamic1”:
Profile.MasterPageFile = “Dynamic1.master”;
break;
case “Dynamic2”:
Profile.MasterPageFile = “Dynamic2.master”;
break;
}
}
MasterPageFile = Profile.MasterPageFile;
}
</script>
<asp:Content
ID=”Content1”
ContentPlaceHolderID=”ContentPlaceHolder1”
Runat=”Server”>
Select a Master Page:
<ul class=”selectMaster”>
<li>
<a href=”DynamicContent.aspx?master=Dynamic1”>Dynamic Master 1</a>
</li>
<li>
<a href=”DynamicContent.aspx?master=Dynamic2”>Dynamic Master 2</a>
</li>
</ul>
</asp:Content>
The page contains two links. Both links include a query string parameter
named master, which represents the name of a Master Page. When you click the first link,
the Dynamic1.master Master Page is loaded and when you click the second
link, the Dynamic2.master Master Page is loaded.
Notice that the page includes a Page_PreInit() event handler. This
handler grabs the value of the master query string parameter and assigns the value of this
parameter to a Profile property. Next, the value of the Profile property is assigned to
the page’s MasterPageFile property. Assigning a value to the MasterPageFile property
causes a Master Page to be dynamically loaded.
Because the name of the Master Page is assigned to a Profile property, the selected Master
Page loads for a user even if the user returns to the website many years in the future. The
Profile object automatically persists the values of its properties for a user across multiple
visits to a website. The Profile is defined in the web configuration file:
<configuration>
<system.web>
<profile>
<properties>
<add
name=”MasterPageFile”
defaultValue=”Dynamic1.master” />
</properties>
</profile>
</system.web>
</configuration>
Loading Master Pages Dynamically for Multiple Content Pages
In the previous section, you learned how to load a Master Page dynamically for a single
page in a website. However, what if you need to load a Master Page dynamically for every
content page in a website?
The easiest way to apply the same logic to multiple content pages is to create a new base
Page class.
using System;
using System.Web.UI;
using System.Web.Profile;
public class DynamicMasterPage : Page
{
protected override void OnPreInit(EventArgs e)
{
this.MasterPageFile = (string)Context.Profile[“MasterPageFile”];
base.OnPreInit(e);
}
}
The class DynamicMasterPage inherits from the Page class. However, it overrides the base Page
class’s OnPreInit() method and adds the logic for loading a Master Page dynamically.
After you create a new base Page class, you need to register it in the web configuration file.
The web configuration file contains the necessary settings.
<configuration>
<system.web>
<pages pageBaseType=”DynamicMasterPage” />
<profile>
<properties>
<add
name=”MasterPageFile”
defaultValue=”Dynamic1.master” />
</properties>
</profile>
</system.web>
</configuration>
After you register the DynamicMasterPage class as the base Page class, every page in your
application automatically inherits from the new base class. Every page inherits the new
OnPreInit() method and every page loads a Master Page dynamically.
Wow, I never knew that Loading Master Pages Dynamically in ASP.NET 3.5. That’s pretty interesting...
Wow, I never knew that Loading Master Pages Dynamically. That's pretty interesting...