ASP.NET Tip: Using Nested Master Pages

Master pages are the best solution for creating a Web site that has shared elements, such as headings and navigation bars. Sometimes you may want to have one set of navigation for one part of your Web site and another set for a "subsite" within the site. In my case, the bulk of my Web site uses one set of navigation, but my online store needs to use the entire sidebar for its own navigation. Master pages provide the ability to "nest" content placeholders in order to make this work.

To implement a nested master page, you create a "root" master page for general use in the site. Here's an example:

<%@ Master Language="C#" AutoEventWireup="true"
    CodeFile="Root.master.cs" Inherits="RootMasterPage" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <link href="/styles.css" rel="stylesheet" type="text/css" />
   <title>Master Page Title</title>
</head>
<!-- rest of your HTML goes here -->
<asp:ContentPlaceHolder ID="toolbarContent" runat="server">
<!-- default toolbar HTML goes here -->
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="rootBodyContent" runat="server" />

</html>

Any HTML in the toolbarContent placeholder will display if another page doesn't provide content. Think of it as the default value for your content. The page's content goes into the rootBodyContent placeholder tag.

The secondary master page looks like this:

<%@ Master Language="C#" AutoEventWireup="true"
    CodeFile="Store.master.cs" Inherits="StoreMaster"
    MasterPageFile="~/Root.master" %>
<asp:Content ContentPlaceHolderID="toolbarContent" runat="server">
<br>
<a class="sidebar_heading" href="/cornerstore/">
   The Corner Store™</a><br>
<!-- rest of new toolbar content goes here -->
</asp:Content>
<asp:Content ContentPlaceHolderID="rootBodyContent" runat="server">
    <asp:ContentPlaceHolder ID="bodyContent" runat="server" />
</asp:Content>

Any page using the secondary master page would put its content into the bodyContent placeholder, which in turn would be put into the overall rootBodyContent placeholder that was defined in the primary master page. The wiring happens in the Master directive at the top of this file, where this master page references the "root" master page.

This is a great way to manage the layout of your site without duplicating content in lots of places. Use the master pages to create a hierarchical structure to the layout of your site and save your self lots of maintenance time down the road.

About the Author

Eric Smith is the owner of Northstar Computer Systems, a Web-hosting company based in Indianapolis, Indiana. He is also a MCT and MCSD who has been developing with .NET since 2001. In addition, he has written or contributed to 12 books covering .NET, ASP, and Visual Basic. Send him your questions and feedback via e-mail at questions@techniquescentral.com.



Comments

  • +33

    Posted by 3232 on 04/06/2013 05:08am

    321322

    Reply
  • Nested Master Pages Minor Issue

    Posted by Haywire on 11/05/2006 07:51pm

    The only problem I have found so far with nested master pages is the fact that you cannot use the design view to create the page content.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • With 81% of employees using their phones at work, companies have stopped asking: "Is corporate data leaking from personal devices?" and started asking: "How do we effectively prevent corporate data from leaking from personal devices?" The answer has not been simple. ZixOne raises the bar on BYOD security by not allowing email data to reside on the device. In addition, Zix allows employees to maintain complete control of their personal device, therefore satisfying privacy demands of valued employees and the …

  • Managing your company's financials is the backbone of your business and is vital to the long-term health and viability of your company. To continue applying the necessary financial rigor to support rapid growth, the accounting department needs the right tools to most efficiently do their job. Read this white paper to understand the 10 essentials of a complete financial management system and how the right solution can help you keep up with the rapidly changing business world.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds