Home > ASP.NET 2.0 > Tips for Nested Master Pages and VS 2005 Design-Time

Tips for Nested Master Pages and VS 2005 Design-Time

Tuesday, 13 May, 2008 Leave a comment Go to comments

There is a cool tip/trick, though, that will allow you to load a design-surface for a page that uses nested master-pages. This will allow you to use all of the control-designers and smart-tasks in design-view for the page (for example: to perform data-binding, auto-format things, and use any of the control wizards), and not have to change any-code to test it at runtime.

The tip/trick works by adding a base-page class to your project or solution that defines a new property called “RuntimeMasterPageFile” of type string. The base-class then overrides the page’s “OnPreInit” method and uses this property to set the Page object’s MasterPageFile at runtime:

public class BasePage : System.Web.UI.Page {
private string runtimeMasterPageFile;
public string RuntimeMasterPageFile {
get {
return runtimeMasterPageFile;
set {
runtimeMasterPageFile = value;
protected override void OnPreInit(EventArgs e) {
if (runtimeMasterPageFile != null) {
this.MasterPageFile = runtimeMasterPageFile;

Once a code-behind class derives from this base class, it becomes possible for a developer to set this “RuntimeMasterPageFile” property in the .aspx file’s <%@ Page %> directive (note: the CodeFileBaseClass also needs to be set to the base class to enable this):

<%@ Page Language="C#"
MasterPageFile="~/Test.master" RuntimeMasterPageFile="SubMaster.master" CodeFileBaseClass="BasePage"
Title="My Page Title" %>

You’ll notice that the above page now has two master-page file directives – the usual “MasterPageFile” directive that VS will use at design-time, and then the custom RuntimeMasterPageFile directive that will override this at runtime. What this allows you to do is point the page at a “test.master” file at design-time that is a single-nested page with contentplaceholder controls that match the names in your nested master. You can then switch to design-view and edit the content and all controls on your page. When you run the page, though, it will use the nested “submaster.master” (or any other master page you specify) to run.

Update: Brian also pointed out to me that you can leave the MasterPageFile attribute value as “” and the designer will then automatically render a test master page that just lists all of the content-placeholder controls in editable regions. This might allow you to avoid having to define a test master page for your site.

Hope this helps and is useful,


  1. Tuesday, 13 May, 2008 at 6:12 pm

    public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)

    do not forget paste this code instead of above:
    public partial class _Default : BasePage
    protected void Page_Load(object sender, EventArgs e)

  2. Wednesday, 14 May, 2008 at 2:38 pm

    Сәлем, сіздің блогты қазақша блогтар қатарына қосып қойып едім, бірақ қазақша жазбадан гөрі, ағылшыншаңыз көп 🙂 Бұл туралы өзіңіз не ойлайсыз?

  3. Wednesday, 14 May, 2008 at 4:39 pm

    Жоқ бұл қазақша блог бола алмайды. Бұнда тек кодтар жазылады, бәрін қазақшаға аударып жату мүмкін емес.

  4. Tuesday, 20 January, 2009 at 3:45 pm

    they say VS2008 supports Nested Master Pages.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: