Loading Master Pages Dynamically


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.

For example, the content page in Listing 5.20 dynamically loads one of two Master Pages named Dynamic1.master and Dynamic2.master.

Listing 5.20. DynamicContent.aspx

<%@ Page Language="VB" MasterPageFile="~/Dynamic1.master" %> <script runat="server">     Protected  Sub Page_PreInit(ByVal sender As Object, ByVal e As EventArgs)         If Not Request("master") Is Nothing Then             Select Case Request("master")                 Case "Dynamic1"                     Profile.MasterPageFile = "Dynamic1.master"                 Case "Dynamic2"                     Profile.MasterPageFile = "Dynamic2.master"             End Select         End If         MasterPageFile = Profile.MasterPageFile     End Sub </script> <asp:Content          ContentPlaceHolder     Runat="Server">     Select a Master Page:     <ul >         <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 in Listing 5.20 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 (see Figure 5.7) and when you click the second link, the Dynamic2.master Master Page is loaded (see Figure 5.8).

Figure 5.7. Displaying the Dynamic1 Master Page.


Figure 5.8. Displaying the Dynamic2 Master Page.


Notice that the page in Listing 5.20 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 contained in Listing 5.21.

Listing 5.21. Web.Config

<?xml version="1.0"?> <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. The file in Listing 5.22 contains a new base Page class named DynamicMasterPage.

Note

Add the file in Listing 5.22 to your application's App_Code folder.


Listing 5.22. DynamicMasterPage.vb

Imports Microsoft.VisualBasic Public Class DynamicMasterPage     Inherits Page     Protected Overrides Sub OnPreInit(ByVal e As EventArgs)         Me.MasterPageFile = CType(Context.Profile("MasterPageFile"), String)         MyBase.OnPreInit(e)     End Sub End Class 

The class in Listing 5.22 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 in Listing 5.23 contains the necessary settings.

Listing 5.23. Web.config

<?xml version="1.0"?> <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.




ASP. NET 2.0 Unleashed
ASP.NET 2.0 Unleashed
ISBN: 0672328232
EAN: 2147483647
Year: 2006
Pages: 276

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net