Preferred Language:

Listing 5.22 - DynamicMasterPage.cs

Illustrates how to create a custom base page class for an application that loads different master pages dynamically.

Listing 5.22 - DynamicMasterPage.cs (C#)
Copy

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 following web.config file sets up the custom base page class.

Listing 5.23 - Web.Config
Copy

<?xml version="1.0"?>
<configuration>
    <system.web>

        <pages pageBaseType="DynamicMasterPage" />

        <profile>
            <properties>
                <add
                  name="MasterPageFile"
                  defaultValue="Dynamic1.master" />
            </properties>
        </profile>
    </system.web>
</configuration>