Freigeben über


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

  • Anonymous
    December 06, 2008
    PingBack from http://itquestions.info/tip-33-did-you-know%e2%80%a6-how-to-create-nested-master-pages-using-vs-2008/

  • Anonymous
    December 07, 2008
    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

  • Anonymous
    December 11, 2008
    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-

  • Anonymous
    January 06, 2009
    The comment has been removed

  • Anonymous
    December 27, 2011
    This article was so helpful. Thanks a bunch.