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: September 19, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT In response to the rising number of data breaches and the regulatory and legal impact that can occur as a result of these incidents, leading analysts at Forrester Research have developed five important design principles that will help security professionals reduce their attack surface and mitigate vulnerabilities. Check out this upcoming eSeminar and join Chris Sherman of Forrester Research to learn how to deal with the influx of new device …

  • Specialization and efficiency are always in need. Whether it's replacing an aging roof, getting a haircut, or tuning up a car, most seek the assistance of trusted experts. The same is true in the business world, where an increasing number of companies are seeking the help of others to administer their IT systems and services. This special edition of Unleashing IT highlights a new breed of IT caretaker -- Cisco Powered service providers -- and the business advantages and operational efficiencies they …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds