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

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: November 13, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT APIs can be a great source of competitive advantage. The practice of exposing backend services as APIs has become pervasive, however their use varies widely across companies and industries. Some companies leverage APIs to create internal, operational and development efficiencies, while others use them to drive ancillary revenue channels. Many companies successfully support both public and private programs from the same API by varying levels …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds