Using Nested Master Page in ASP.NET

Master page made the developers life extremely easy where all of the core design (specially headers, footers etc) can be designed and defined in one page which then can be inherited by all the pages in the application without requiring adding the header, footers etc explicitly. However master pages can also be nested, that is in a application, where are there two (or more) set of designs to be followed in two or more set of pages, along with following some common design components (such as header, footer etc) among all the pages in the system. In that case, we will create a main master page which will contain all common design components, and then will create separate nested master pages that will includes specialized designs for specific sets of pages.

Here is a sample for the main master page:

<%@ Master %>
<html>
<head id="Head1" runat="server">
    <title>Title</title>
</head>
<body>
    <form id="form1" runat="server">
        <div class="content">
            Some content in main master page
            <div >
                <asp:ContentPlaceHolder ID="ContentPlaceHolder1" 
                runat="server">
                    Child master page will be placed here...
                </asp:ContentPlaceHolder>
            </div>
        </div>
    </form>
</body>
</html>

Here is a sample for the nested master page:

<%@ Master MasterPageFile="~/master/employee.master"%>
<asp:Content ID="Content1" runat="server" 
ContentPlaceHolderID="ContentPlaceHolder1">
    <table width="100%">
        <tr width="100%">
            <td width="100%">
                This is child master page<br />
            </td>
        </tr>
        <tr width="100%">
            <td width="100%">
                <asp:ContentPlaceHolder ID="ContentPlaceHolder1" 
                runat="server" EnableViewState="true">
                Contents of pages...
                </asp:ContentPlaceHolder>
                <br/>
                Content in Child master page.
                <br/>
                <asp:ContentPlaceHolder ID="ContentPlaceHolder2" 
                runat="server" EnableViewState="true">
                Contents of pages.
                </asp:ContentPlaceHolder>
            </td>
        </tr>
    </table>
</asp:Content>

Here is a sample page that uses the nested master page:

<%@ Page Language="VB" MasterPageFile="~/master/default.master"
Title="Using inherited master page..." %>

<asp:Content ID="Content1" 
ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <div class="contentcontainerDIV">
        <div class="headerDIV">
            <br />
            <h3>
                Some content on page
            </h3>
            <p class="ContentText">
                Some content on page
            </p>
        </div>
    </div>
</asp:Content>
<asp:Content ID="Content2" 
ContentPlaceHolderID="ContentPlaceHolder2" runat="Server">
    <div class="contentcontainerDIV">
        <div class="headerDIV">
            <br />
            <h3>
                Some content on page
            </h3>
            <p class="ContentText">
                Some content on page
            </p>
        </div>
    </div>
</asp:Content>

Download sample:

Technorati Tags: ,,,,

Print | posted on Monday, February 18, 2008 5:47 PM

Comments on this post

# re: Using Nested Master Page in ASP.NET

Requesting Gravatar...
Ashraful its very nice to learn...
am happy to use
regards
j.kasim mohamed
Left by Kasim Mohamed on Apr 14, 2008 8:26 PM

# re: Using Nested Master Page in ASP.NET

Requesting Gravatar...
Nice Article.

Thanks
Regards,
Rajni Padhiyar
Software Engineer
Left by Rajni Padhiyar on May 23, 2008 12:45 PM

# re: Using Nested Master Page in ASP.NET

Requesting Gravatar...
Nice article.
Left by Rajni Padhiyar on May 23, 2008 12:46 PM

Your comment:

 (will show your gravatar)
 
Please add 1 and 6 and type the answer here: