VS 2008 Nested Master Page Support

"Master Page" support was one of the most popular features introduced with ASP.NET 2.0, and is one of those features that almost every new ASP.NET project now uses to deliver consistent layout template support across a web site.

One of the cooler scenarios that ASP.NET 2.0 supports is the ability to have "nested master pages" - where you can create a root master page for a site that defines a site's overall layout, and then create nested master pages that are based on the root master and further customize the layout (for example: you could create a SingleColumn.Master and TwoColumn.Master that defined 1 or 2 column layout structures based on the root master template).  This nested master page feature is extremely flexible, and enables developers and designers to quickly and very cleanly make changes to the layout and organization of a site with minimal code changes and no content duplication.  The one (big) catch, though, is that Visual Studio 2005 doesn't really support using nested master pages, and pages based on nested masters can't be edited within the VS 2005 WYSIWYG designer. 

The good news is that VS 2008 fully supports nested master pages, and makes using them super easy.  Going forward I recommend that almost all ASP.NET projects should advantage of this feature - since it can add tremendous flexibility to the UI of your projects.

Using Nested Master Pages With VS 2008

One of the sites I recommend checking out is http://www.opensourcetemplates.org/.  This is an online repository of free HTML site templates that you can browse, download and use.  The templates on the site are pure HTML (meaning you can use them with any server-side programming technology), and are built using clean CSS and XHTML markup:

To help with this blog post, I picked one of the templates that I thought looked nice.  You can browse it online here, and download it here

Like most web-sites out there, this template has pages that utilize a few different layout approaches for displaying content.  For example, it includes a page that uses multiple-columns to layout content:

It also has pages that use a single column approach that fills the entire width of the page with content:

Using the Above HTML/CSS Template with Nested Master Pages

Converting the above template to use ASP.NET and its nested Master Page support is really easy with VS 2008. 

Step 1: Create a Site.Master template

To begin with we can create a new root master page file that we'll use to define the overall layout and structure for all pages on the site.  We'll name this file "Site.Master" and copy/paste the "outer chrome" HTML from the template we downloaded into it.  We'll then add a <asp:contentplaceholder> into the content section in the middle where we'll fill in page specific content.  We'll name this <asp:contentplaceholder> control "MainContent":

Step 2: Create SingleColumn.Master Template

We used our Site.Master template above to define the "outer chrome template" of the site we are working on.  We'll now want to create a few nested master pages that provide further templates that customize the layout of the "MainContent" section.

To do this we can right-click in the solution explorer and choose the "Add New Item" menu option.  We'll create a new Master Page called "SingleColumn.Master" and will want to make sure that the checkbox indicating that we want to base this master page off a parent master page is selected:

VS 2008 will then allow us to pick the Site.Master template as the master page we want to base this new SingleColumn.master template on:

VS 2008 will then create a new SingleColumn.master file that looks like below:

Notice above how VS 2008 has picked up the fact that we defined a <asp:contentplaceholder> control in the root Site.Master file called "MainContent" - and automatically added a blank <asp:content> control within this nested master page so that we can override and customize that content region.

Also notice above how we can use the new "Split View" support within VS 2008 to enable us to see both the HTML source and WYSIWYG designer surface at the same time.  We can make changes within either of the split views, and immediately see the changes applied in the other (hint: make sure to show your boss this new feature when asking for a bigger monitor). 

Our SingleColumn.Master template is going to end up being really simple to begin with, and will just add a CSS rule to define the width of the single column of content - and then add its own <asp:contentplaceholder> control so that pages based off of this master can fill in their page specific content:

Step 3: Create TwoColumn.Master Template

In addition to having our SingleColumn.Master template, we'll want to create a second nested master page template to handle two columns of content.  We'll repeat the steps we followed above - but this time call the file "TwoColumn.Master".  We'll define the layout of this file like below:

Notice above how we have added two <asp:contentplaceholder> controls within this nested master page - one called "MainColumn" (just like the SingleColumn.Master), and the other called "LeftColumn" (to handle a smaller column of content that will be displayed on the left of the page).  We'll use standard HTML and CSS to position these two columns.

Step 4: Creating Content Pages using our Nested Master Pages

Now that we've defined master pages to control our site's layout structure, we can start adding pages to the site. 

Let's create a new page that we'll call "HomePage.aspx" - and indicate that we want it to be based on a master page:

We can then choose which master page or nested master page we want to base it on.  For the home-page let's use the two column layout:

VS 2008 will then base HomePage.aspx on this nested master file, and add two empty <asp:content> controls to the page that allows us to fill in the left and main column content for the page:

Notice above how we not only get WYSIWYG support for pages built using nested master pages, but we can also use split-view with them.

I can then fill in the specific content I want into the appropriate <asp:content> regions, and add any code I want to the code-behind:

I can later add additional pages to the project, and base them on either the SingleColumn.Master or TwoColumn.Master template. 

Because both of these templates are based on a root Site.Master template, if I make any changes to this root template (for example: I want to change the top logo or navigation structure), it will automatically apply to all pages within the site without me having to modify them at all.

I also now have the flexibility to change the layout of the SingleColumn or TwoColumn templates (for example: adjusting their width) and all of the pages on the site that are built with the modified master templates will immediately have the changes applied.

Summary

VS 2008 has full support for using ASP.NET nested master pages (this feature is also fully supported with the free Visual Web Developer Express 2008 edition).  I think you'll find that this makes building consistent UI and site layout much easier. 

Best of all, because VS 2008 now has multi-targeting support you'll be able to use the above feature immediately without having to install .NET 3.5 on your servers.  You can open existing ASP.NET 2.0 projects in VS 2008, have VS 2008 continue to target .NET 2.0 as the runtime target, and start using this feature.

Hope this helps,

Scott

52 Comments

  • Very good news, Scott :-)

  • Scott, when do you guys plan to release VS 2008 Beta 2 with a GoLive License? Cheers, Andreas

  • Excellent. Now, if I can just find a way to complete disable the design view, I'll be happy :)

    (in 2008 can you right-click on a .aspx.vb/.cs file and select "View Markup" or is "View Designer" still the only option?)

  • Nice work matey! I really missed this in VS2005 and was a bit in shock that the framework supported it but the IDE didn't. So this post really wasn't unexpected I suppose... just good to hear that you didn't forget it in VS2008 either. Good work with your community stuff too Scott. You are the glue between MS and the community. I'm going to call you "ScottGlue" from now on...

  • Here's a critical note, I hope you will address it.

    To me it seems unfair that we should buy a new version of Visual Studio to get functionality that (in my honest opinion of course) should have been included in Visual Studio 2005. Nested master pages as well as Intellisense in .skin files seem like oversights that should be addressed in a Service Pack.

    Of course I will ask my boss to upgrade to Visual Studio 2008, and of course I'm looking forward to all the new features, and I expect VS 2008 to be worth the upgrade. But I can't help to feel cheated out of a reasonable functionality in the current version of VS 2005. I sincerely hope you will comment on this, thanks!

  • Man I can't wait to start using Orcas.

  • Will sub-web-projects be supported? I know you can do it in 2005 but if feels like a hack and you get some problems with it.

  • Glad to see this support coming. How soon before a better mvc pattern for asp.net comes out?

  • That's awesome! Now, if we would only have something like this but for UserControls that would be super. Imagine having a UserControl act like a template, or like a MasterPage in this case. I could place ContentPlaceHolders inside the UserControl and allow the calling page to customize it. That would be super useful; I can't tell you how many times I wished I had a feature like this...

  • Hi Andreas,

    VS 2008 Beta2 should be available for free download in the next three weeks. It will support a go-live license.

    Thanks,

    Scott

  • Sorry for getting little off-topic on the subject of nested master pages, but, as I already posted in asp.net forum, there is a problem in design view of VS "Orcas" Beta 1 with editing of content page where master page is applied at a folder level.

    For example web.config file contains following sections:








    ....

    When I open content page located in the folder named "Subfolder" in design view
    I have it enclosed by master page applied at the application level (Main.master) instead
    of the one I specified for the given folder (Subfolder.master).
    At run time correct master page is applied though.

    Thanks,
    Alexei

  • Hi Boris,

    You can right-click on a page within the solution explorer and select "View Markup", "View Designer", "View Code" and "View Component Designer" to load just the designer you are looking for.

    You can also go into tools->options with VS 2008 and select what you want the default view to be when you double-click on a .aspx file. You can select between source, design or split view.

    Thanks,

    Scott

  • Hi Mike,

    That is a fair question. In general we try and splip-stream small feature enhancements and improvements into service packs. You'll see us get more regular about doing this going forward.

    Nested master page support was a pretty big feature in terms of implementation, though, which is why we couldn't get it added to either VS 2005 or VS 2005 SP1 (if we could have got it done in either of those releases we would have).

    We've actually completely replaced the ASP.NET/HTML designer with VS 2008 - and now use the same designer infrastructure that Expression Web uses today. This took us about 3.5 years of coding to do and brings with it some pretty significant feature improvements (I'll be blogging about some of the CSS improvements shortly).

    In terms of upgrading from VS 2005, we will have an upgrade priced version of the product which should allow you to get the new version pretty reasonably (I don't know the exact prices just yet). MSDN subscribers will also get the upgrade for free.

    Hope this helps explain things a little,

    Scott

  • Hi Torkel,

    Unfortunately we won't have significant new support for sub-web projects with the initial VS 2008 release. This is a feature we really wanted to add, but ultimately ran out of time to-do for the initial release (it works the same as VS 2005). However, the team has it very high on the list of potential features to add into SP1 when it comes out.

    Hope this helps,

    Scott

  • Hi Billie,

    The ASP.NET team is working on a MVC pattern right now that we'll be talking about a little later this year. You can use .aspx pages as views with this approach - which means that features like nested master pages will fully work with it.

    Thanks,

    Scott

  • Hi Peter,

    I definitely agree that templatized user controls (with a master page like layout support) would be big. There is actually runtime support for adding templates to user controls today - but unfortunately we don't have WYSIWYG designer support yet. Something on the feature list for the future though!

    Thanks,

    Scott

  • This is excellent news! Finally I'll be able to use design mode again. It has been so long since I've been able to because I always use nested masterpages.
    I do think that almost every web site should use nested masterpages.

  • I can't wait for beta 2!!!

  • Hi Scott. Are there any plans to implement cross application master pages? There are many times where I have had to develop an application as part of a larger website where it would have been very convenient to use a master page from the root application in the sub application. I know there is a way to do this currently, but its almost to much trouble to be worth it, and it feels very much like a hack.

  • Hi Mike;
    VS2008 has so many new features that I wouldn't bother thinking what VS2005 doesn't have or should have. That's like water under the bridge...
    Just be happy that we'll have it now and enjoy it from here on.
    ..Ben

  • wow, another great blog post. thanks this was really missing in vs 2005

  • Hi Scott,

    Yet another nice addition in an already impressive list of new, exciting features for Visual Studio 2008. It seems VS2008 will be an even bigger step forward from VS2005, than VS2005 was from VS2003. Keep up the good work ;)

    Kind regards, Rick.

  • Hi Alexei,

    Thanks for forwarding this issue - someone on the team is now looking at how to add support for this.

    Thanks,

    Scott

  • I cannt wait for beta2 tooo
    thanks

  • Hi Zack,

    The June CTP is legit - although not the final Beta2 build. The final Beta2 build should be out later this month - I'll definitely be bloging an announcement of it once it is ready.

    Thanks,

    Scott

  • Scott,
    Great tool and I'll surely save the opensourcetemplates.org site in my favorites.
    Thanks for posting...

  • A great post. I am looking for VS 2008 release version.

  • Scott, regarding the MVC pattern that you'll be talking about later this year, will it be included in VS2008/.Net 3.5? Will there be a beta?

    I really hope the MVC work is prioritized.

  • Has your team ever though of supporting a design-time masterfile property? At run-time we decide what interface we are presenting to this particular user and set the masterpage property programmatically if it's not already set. It would be cool if there was a way to set a design-time masterpage giving us the ability to use the editor again while not having it affect the run-time logic.

  • Are there non-VPC version?

  • Cool, I love VS 2008.

  • Good Day Scott,

    I am just curious to know more about this line you have mentiond in this blog responses...

    ----------------------------------------------
    The ASP.NET team is working on a MVC pattern right now that we'll be talking about a little later this year.
    ---------------------------------------------

    Could you kindly focus a little more on this.

    Thanks

  • Scott,
    Thanks for your exciting news! Would you like to tell me when the released days for vs 2008?

  • Hi Josh,

    That is a good suggestion. One other option might be to use the standard MasterPageFile attribute for design-time, and then always programmatically set it at runtime. Would that also work for your scenario?

    Thanks,

    Scott

  • Hi Softmind,

    What we announced at the partner conference was that VS 2008 would launch in Feb of 2008. "Launch" for VS means that is when we schedule our big events around the world to talk about the new product.

    Our hope is to release the product earlier than that - in which case it will be availble for MSDN subscribers to download as well as for people to buy prior to the launch events. We haven't announced a target date for this yet - but I suspect it will be several weeks or a few months before the launch event.

    Hope this helps,

    Scott

  • Hi there,

    A friend of mine emailed me this blog entry.
    I run opensourcetemplates.org, and a big thank you for the kind words on my designs and CSS/XHTML

    Just a quick update about the site, we have just finished the login function so there will be a TON of new website tempates from good artists from around the world showing up on the site very soon.

    Thanks again for the kind words
    Cheers
    Aran
    Site: studio7designs.com

  • This is great news, now if ASP.NET could just get User Control Designer support in class library projects, VS-life would be sweeter still!

  • will WebPart and UpdatePanel in VS 2008 be compatible together? (WebParts in UpdatePanel)

  • Love master page and looking forward to new version 2008 - when does it come out(MSDN)?

    Also, how do you dynamically add controls to the content page? I have a table in the master for header, left navigation bar, body (content for detail pages) and footer.

    When I .add the controls on the load event of the content page, all the control are added after the table renders?

    Any ideas?

  • Also - forgot to add. Whoever at microsoft designed the visual studio service packs should be shot. Never saw some much trouble adding a service pack (runs for 1 hour to tell you that something in the keys is confused).

    Why can't you slipstream this #$@$## for us so we can download via MSDN the patched version and offer service packs too for those who want to waste days of their lives they will never get back.

  • Is WebDeployment going to be part of the next beta?

  • Nested Master Page Support is a great feature.But please make VS 2008 fast.Not everone in the word have ultra high end machines.

  • Will there be any future release of asp.net to offer support for (nested) master controls?

  • MVC for ASP.NET can't wait to see how this turns out!

  • Scott,

    I just downloaded Beta 2 and tried to follow your instructions but I have no option for selecting master page file when selecting to add a new item and choose master page. Also odd is that when I choose to add a content page I get pushed to a second screen to then select the master.

    Now, the web app i'm playing with i created by creating a C# project, choosing the web set of options, and selected ASP.NET Web Site. This seems markedly different from when I go to New -> Website. If I were to create a site that way would I then have the option to assign a master to a master page?

    And i haven't seen anything definitive on this yet - what is the recommended way to create a web app - is it through the project creation function or the new web site creation function? which will be pushed/evolved in the future or does the team plan on keeping two separate methods? if so, aside from individual aspx pages not being compiled in the web application projects (read that online somewhere) what's the difference? I'm sure this answer has been hashed and rehashed a million times - a good link pointing me in the right direction would be awesome.

    Thanks! AND GREAT WORK WITH BETA 2!!!! Been playing with my copy alllll day :).

  • Very good feature, thanks :)

  • Hi
    I try it in VWD 2008 Express Beta 2
    The weppage works but the VWD IDE reports an error in the design view.
    (The Page has one ore more controls that do not correspond with controls in the Master Page.

  • Hi swo and Jim,

    Can you send me an email (scottgu@microsoft.com) with details about the error you are seeing? It sounds kinda odd - you shoudl be running into that.

    Thanks,

    Scott

  • Hi Scott,

    Thanks for your reply on this thread.

    You misunderstand though - the options are there if you right-click an .aspx file. But not if you right-click the code-behind (i.e. aspx.vb or aspx.cs). Then you can only View Designer. It's the only option. Always seemed a little odd to me that the options were different, and I really like using that right-click to open the markup!

    - Zombie

  • Referring to the comment by Michael Raumer, I found the same behavior. It took me a while as there is no mention of the different behavior in your excellent article. The steps in the article cannot be followed if the user created a ASP.NET Web Application by selection File->New->Project->ASP.NET Web Application. It only works if the user created a ASP.NET Website by selecting File->New->Web Site->ASP.NET Web Site.

    I am just curious if both the project templates are required in future. If yes, I would like to know the reasons. They are very similar but different in confusing ways...

    By the way, I am an ardent fan of your blog and your writings. Great work, Scott.

  • Hi, ScottGu

    I also encount the problem that Visual Studio 2008 TS reports an error:
    the page has one or more controls that do not correspond with controls in the Master Page.

  • Hi Scott,
    I am having the same problem with the Beta 2 that dwo, cheney et.al. are having.
    To reproduce this error:-
    1) Create new ASP.Net Web Application
    2) Add new Master Page
    3) either rename one of the ContentPlaceHolders on the Master page, or add a new one.
    4) Add a Content page for this Master Page.
    voila - you should be seeing the "page has one or more controls that do not correspond with controls in the Master Page." error in the IDE.
    The page runs fine, but you can't use the visual designer!

    Any ideas?

    Keith...

Comments have been disabled for this content.