Nested Master Pages Until Orcas

This article describes how to inherit your .aspx pages from a custom class, in such a way that you can use nested master pages, without getting that terrible 'Design view does not support creating or editing nested master pages. To create or edit nested master pages, use Source view' from the Visual Studio Designer.

Just before posting this article on my blog I came across an article from Scott Guthrie who describes exactly the same issue with almost exactly the same solution. Even the RuntimeMasterPageFile property has the same name. D'oh!.

Scott's solution works fine for aspx files, however it doesn't help us with designing master pages. We could try to write a custom class that inherits from the MasterPage class in which you can implement the same trick as done in Scott's article with pages. In that case you can't use the PreInit event, because master pages simply don't have a PreInit event. You must choose a different path. Instead you should override the MasterPage's protected SupportAutoEvents property. This property is the first overrideable point within the MasterPage class that is called after the master page has been created by it's owner (Thanks to Reflector). Within this property you can set the master page's MasterPageFile property. The class may look like this:

public class NestedBaseMaster : MasterPage
private bool _addMaster = true;
private string _runtimeMasterPageFile;

public string RuntimeMasterPageFile
get { return this._runtimeMasterPageFile; }
set { this._runtimeMasterPageFile = value; }

protected override bool SupportAutoEvents
if (this.Page != null && this._addMaster)
if (this.RuntimeMasterPageFile != null)
this.MasterPageFile =
this._addMaster = false;

return base.SupportAutoEvents;

This however, will lead to nothing. Visual Studio will still show that nasty message and disallows designing your master page. Nested master pages simply can't be edited and there is no work-around for this. For the real solution we must wait until Orcas is released. A lot of exiting features will be added in the Orcas release of Visual Studio and ASP.NET, so I can barely wait.

- ASP.NET, Visual Studio - No comments / No trackbacks - §

The code samples on my weblog are colorized using javascript, but you disabled javascript (for my website) on your browser. If you're interested in viewing the posted code snippets in color, please enable javascript.

No comments:

No trackbacks:

Trackback link:

Please enable javascript to generate a trackback url

Remember personal info?


Before sending a comment, you have to answer correctly a simple question everyone knows the answer to. This completely baffles automated spam bots.

  (Register your username / Log in)

Hide email:

Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.