Tips for Nested Master Pages and VS 2005 Design-Time
page 1 of 3
Published: 11 Nov 2005
Unedited - Community Contributed
Abstract
In this article, Scott provides tips for creating a nested master page using Visual Studio 2005.
by Scott Guthrie
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 21628/ 67
Article Contents:

Introduction

Republished With Permission - Original Article

One of the cool advanced features of the new Master Pages feature in ASP.NET 2.0 is the ability to nest them.  For example, you could define a top-level master-page called “MainMaster.master” that defines a common logo header and footer, and defines a content-placeholder for the page called “content”.  You could then define two sub-masters beneath it – one that provides a two-column layout model for the content (“TwoColumnMaster.master”), and one that provides a three-column layout model (“ThreeColumnMaster.master”).  These two nested-master pages could then use the MainMaster.master file as the root master, and only adjust the layout within its content section (each would fill in the "content" placeholder and in turn add their own content-placeholders for sub-pages within it).  The benefit of this approach is that if a designer ever changed the logo or top-level design of the site, they only have to update one file (MainMaster.master) and then have every page on the site automatically pick it up regardless of what master file they were based on.

This nested model is pretty cool and powerful, and allows arbitrary levels of nesting.  The only downside is that VS 2005 only supports nested master page editing in source-view, and doesn’t support it in the WYWSIWYG designer (note: obviously it does support editing them when the master-pages are not nested). 

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:

Listing 1

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;
    }
    base.OnPreInit(e);
  }
}

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):

Listing 2

<%@ Page Language="C#" 
MasterPageFile="~/Test.master" 
RuntimeMasterPageFile="SubMaster.master" 
CodeFileBaseClass="BasePage" AutoEventWireup="true"
 CodeFile="Default.aspx.cs" Inherits="_Default" 
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.


View Entire Article

User Comments

Title: That is a great tip!   
Name: Kim Berry
Date: 2008-01-04 9:10:31 AM
Comment:
I was about to give up on nested masterpages and use controls or something. But Daniel D' Armas tip worked - eg in default.aspx i put:

MasterPageFile="~/MasterPageStaticInner1.master "

and i can view it in design mode, and it functions at run time.
Title: RuntimeMasterPageFile compiler warning   
Name: pam
Date: 2007-11-02 6:50:37 PM
Comment:
This code works great but is there a way to stop the compiler warning "Validation: Attribute 'RuntimeMasterPageFile' is not a valid attribute of element 'Page'?
Title: Helpful Trick   
Name: Daniel D' Armas
Date: 2007-02-09 10:31:02 AM
Comment:
I've seen that clearing the content of the MasterPageFile attribute of the @Page let you view the page in design mode with no styles, but stills being awful for us (Developers) 'cause we have to set it again when we want to run the application, so trying to fix this i found something that could help, if you don't remove the content of the MasterPageFile Attribute but you only add a blank space the Runtime will render exactly like if you removed the content with the exception that you haven't to remove this space when your run your application.

Example: MasterPageFile="Admin.master "

So add a blank space and you're done!.
Title: aay   
Name: jay
Date: 2006-08-19 2:39:10 AM
Comment:
RuntimeMasterPageFile="SubMaster.master"

error

please update your code

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-11-22 1:18:33 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search