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
{
get
{
if (this.Page != null && this._addMaster)
{
if (this.RuntimeMasterPageFile != null)
{
this.MasterPageFile =
this.RuntimeMasterPageFile;
}
}
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 - § ¶