November 24, 2014
Hot Topics:

ASP.NET Tip: Using Nested Master Pages

  • September 15, 2006
  • By Eric Smith
  • Send Email »
  • More Articles »

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.






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel