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.
ASP.NET: Nested Master Pages
From Wiki
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:
- <%@ Master Language="VB" CodeFile="MainMasterPage.master.vb" Inherits="MainMasterPage" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title>Untitled Page</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <p style="color:Blue;">Hello!, I'm the Main Master Page!</p>
- <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
- </asp:contentplaceholder>
- </div>
- </form>
- </body>
- </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:
- <%@ Master Language="VB" CodeFile="SubMasterPage.master.vb" Inherits="SubMasterPage" MasterPageFile="~/MainMasterPage.master" %>
- <asp:Content id="Content1" ContentPlaceholderID="ContentPlaceHolder1" runat="server">
- <p style="color:Red;">Hello!, I'm the Sub Master Page!</p>
- <asp:ContentPlaceHolder ID="ChildContent1" runat="server">
- </asp:ContentPlaceHolder>
- </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.
- <%@ Page Language="VB" MasterPageFile="~/SubMasterPage.master" AutoEventWireup="false" CodeFile="Default1.aspx.vb" Inherits="Default1" title="Untitled Page" %>
- <asp:Content ID="Content1" ContentPlaceHolderID="ChildContent1" Runat="Server">
- <p style="color:Green;">Hello, I'm the content page!</p>
- </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



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