Tip #33: Did you know… How to create Nested Master Pages using VS 2008?


VS 2008 and VWD 2008 both support Nested Master Pages which means you can have a Base Master page which would cater to the whole site and then you can further customize these Base Master Pages.

Here is how you do it:

1. Create a BaseMasterPage.master for the base look:

First we need to create a Base Master Page look, which will apply to the entire site. To do this you will have to Add New Item to your Web Site and Select Master Page Template. Let us name it BaseMasterPage.Master.

BaseMasterPage

Let us say that our BaseMasterPage looks like this:

BaseMasterPageLook

2. Create a NestedMasterPage.master based on the Base Master Page created above.

To do this right click on Solution Explorer of your web site and launch the Add New Item dialog.

In this dialog select Master Page Template and to make this Master page a Nested one, specify the parent Master page by checking the “Select master page” checkbox as shown below.

NestedMasterPage

Click Add in the above dialog. This will automatically launch the “Select a Master Page Dialog” as shown below. In this dialog select your parent/base master page.

Select a Master Page dilaog

Notice that VS will now add a blank <asp:content> control inside the Nested Master Page and this content control can be further customized. This is the same BaseContentPlaceHolder which was available in the parent master page.

BaseContentPlaceHolder in NestedMP

3. Overriding BaseContentPlaceHolder in NestedMasterPage.master.

To override the contents in NestedMasterPage, copy paste the following piece of code inside the BaseContentPlaceHolder

<asp:Content ID="Content2" ContentPlaceHolderID="BaseContentPlaceHolder" Runat="Server">
      <p>
        This is data from Nested Master Page. 
      
</p>
  <asp:ContentPlaceHolder id="ContentPlaceHolderInNestedMP" runat="server">
       
  
</asp:ContentPlaceHolder>
      
</asp:Content>

 

At this stage, your NestedMasterPage will look like this. Also, note that we have added a New ContentPlaceHolder (ContentPlaceHolderInNestedMP) in the Nested Master Page.

NestedMasterPageWithContentPlaceHolder

4. Create content page based on the NestedMasterPage.master

To add a content page using the NestedMasterPage.master, all you need to do is select NestedMasterPage.Master in solution explorer, right-click and select “Add Content Page”. This will add a new content page with an empty ContentPlaceHolderInNestedMP, which could be overridden.

Reshmi Mangalore

SDET, Visual Web Developer

Comments (5)

  1. MisterFantastic says:

    Hi,

    The article is really nice. Where are we using this kind of master pages. Give me some practical examples of using nested master pages.

    Thanks ,

    Thani

  2. My latest in a series of the weekly, or more often, summary of interesting links I come across related to Visual Studio. Channel 9 Stuff: Drag-Drop Data Binding Comes to WPF in Visual Studio 2010 This Week on C9- The Thanksgiving Episode Rico Mariani-

  3. Questioning Minds says:

    Thanks for the tip.  That is what I was looking to do.  The only question I still have is how do you access the code behind?  I not only want the nested master pages to organize the way the pages look, but to also organize the code that goes along with creating that look.  However, since you cant use the inherits attribute, you cant cast the Page.Master property to the specific master or a supported interface. Is there a way around this or is it a limitation of nested master pages?

  4. Venky says:

    This article was so helpful. Thanks a bunch.

Skip to main content