Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Nested Master Pages in ASP.NET 4.0

Author: Jeff Nava

Nested Master Pages feature is provided in ASP.NET 3.5 which is, of course, available in ASP.NET 4.0.

 

In a previous article on DevASP.NET, you can learn what master pages are and how to create master pages in ASP.NET.

Nested Master Pages
 
Master pages are very popular feature to give your web site a consistent look and feel. Now a master page can be nested. This means one master page can be referenced by another master page. You can create a root master page for consistent layout to your entire web application. Then you can create nested mater pages that are based on the root master page and further customize some sections of your web application layout. You can create any number of nested master pages. There is no limit for nested pages.
 
How to create Nested Master Page and its associated Content Page
  1. Open Visual Studio 2010
  2. File > New > Web Site
  3. Choose language Visual C# or Visual Basic
  4. Create New ASP.NET Empty Web Site
  5. Web Site > Add New Item
  6. Select “Master Page” and click “Add”
  7. You can see a page MasterPage.master.  Write code below in MasterPage.master in between <form> tag

     
     
       
    <div style="background-color:Aqua">
       
     
       
      <asp:Panel ID="Panel1" runat="server" Height="50px" HorizontalAlign="Center">
       
       <asp:Label ID="Label1" runat="server" Text="This is Parent Master"></asp:Label>
       
      </asp:Panel>
       
      <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">       
       
      </asp:ContentPlaceHolder>
       
        <asp:Panel ID="Panel2" runat="server" Height="50px" HorizontalAlign="Center">
       
          <asp:Label ID="Label2" runat="server" Text="This is Parent Master"></asp:Label>
       
         </asp:Panel>
       
     </div>
     
  8. Now again Web Site > Add New Item.
  9. Again Select “Master Page”
  10. Check “Select master page” option and click “Add”
  11. “Select a Master Page” window will appear. Select the MasterPage.master and click “Ok”
  12. A Master Page will be created with a MasterPage2.master name.
  13. Write code below in MasterPage2.master in between <asp:Content> tag which has the ContentPlaceHolderID is equal to “ContentPlaceHolder1”. 

    <asp:Panel ID="Panel1" runat="server" Height="75px" BackColor="Silver"
       
        HorizontalAlign="Center">
       
                <asp:Label ID="Label1" runat="server" Text="This is Child Master"></asp:Label>
       
            </asp:Panel>  
       
        <asp:ContentPlaceHolder ID="ChildMaster1" runat="server">
       
        </asp:ContentPlaceHolder>
     
    <asp:ContentPlaceHolder> control is added with an “ID” of “ChildMaster1”.
     
  14. Now again Web Site > Add New Item.
  15. This time select a “Web Form”
  16. Check “Select master page” option and click “Add’
  17.  “Select a Master Page” window will appear. This time select the MasterPage2.master and click “Ok”

    <asp:Content ID="Content1" ContentPlaceHolderID="ChildMaster1" runat="server">
       
        <asp:Panel ID="Panel1" runat="server" Height="100px" BackColor="White"
       
        HorizontalAlign="Center">
       
            <asp:Label ID="Label1" runat="server"
       
                Text="This is Content Page of the Child Master Page"></asp:Label>
       
        </asp:Panel>
       
       
       
    </asp:Content>

    The “ContentPlaceHolderID” in <asp:Content> tag is “ChildMaster1” which is the “ID” of <asp:ContentPlaceHolder> tag in MasterPage2.master.
          
  18. Now Press F5 and see the result in browser.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< What are Master Pages and how to create Master Pages in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net