Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

ASP.NET: Nested Master Pages

From Wiki

Jump to: navigation, search

Summary: Nested Master Pages

Master Pages are a great way of including content on multiple pages of your site without having to rewrite code on every page. Nested Master Pages expands this concept to allow you to not only have one main master, but to have several sub master pages for situations where you maybe have groups of pages that share common styles or functionality.

To create a Master Page, have a look at this simple example:

  1. <%@ Master Language="VB" CodeFile="MainMasterPage.master.vb" Inherits="MainMasterPage" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5.     <title>Untitled Page</title>
  6. </head>
  7. <body>
  8.     <form id="form1" runat="server">
  9.     <div>
  10.         <p style="color:Blue;">Hello!, I'm the Main Master Page!</p>
  11.         <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
  12.         </asp:contentplaceholder>
  13.     </div>
  14.     </form>
  15. </body>
  16. </html>

The ContentPlaceHolder control in the above example dictates how and where the content pages data will be shown. We can now create a sub master page that will place some content in this position:

  1. <%@ Master Language="VB" CodeFile="SubMasterPage.master.vb" Inherits="SubMasterPage" MasterPageFile="~/MainMasterPage.master" %>
  2. <asp:Content id="Content1" ContentPlaceholderID="ContentPlaceHolder1" runat="server">
  3.     <p style="color:Red;">Hello!, I'm the Sub Master Page!</p>
  4.     <asp:ContentPlaceHolder ID="ChildContent1" runat="server">
  5.     </asp:ContentPlaceHolder>
  6. </asp:Content>

Now, when we create a content page, rather than point at the main master page we simply point at the sub master e.g.

  1. <%@ Page Language="VB" MasterPageFile="~/SubMasterPage.master" AutoEventWireup="false" CodeFile="Default1.aspx.vb" Inherits="Default1" title="Untitled Page" %>
  2. <asp:Content ID="Content1" ContentPlaceHolderID="ChildContent1" Runat="Server">
  3.     <p style="color:Green;">Hello, I'm the content page!</p>
  4. </asp:Content>

Which will then give us the very simple output of:


Hello!, I'm the Main Master Page!

Hello!, I'm the Sub Master Page!

Hello, I'm the content page!


This Hack is part of the ASP.NET Hacks collection

492 Rating: 2.9/5 (13 votes cast)