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

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

  • A modern mobile IT strategy is no longer an option, it is an absolute business necessity. Today's most productive employees are not tied to a desk, an office, or a location. They are mobile. And your company's IT strategy has to be ready to support them with easy, reliable, 24/7 access to the business information they need, from anywhere in the world, across a broad range of communication devices. Here's how some of the nation's most progressive corporations are meeting the many needs of their mobile workers …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds