Team LiB
Previous Section Next Section

Nested Master Pages

It's possible to nest Master Pages, though this will certainly be an exception rather than a rule. To do so, you can define an additional (parent) Master Page for an existing Master Page and so on. This approach is very useful for times when you want to modify a comprehensive layout and use it for different areas of your web site.

The example in Listing 4-5 comes from the online help. You can already use this example in the Alpha version. The IDE, however, is still facing some difficulties with regard to nesting Master Pages that will probably be corrected in the Beta version. Figure 4-6 shows the nested Master Pages in action.

Click To expand
Figure 4-6: Both master pages are merged with the Content Page.
Listing 4-5: Nesting Master Pages
Start example
// MasterMasterPage5.master (Parent Master)
<%@ Master Language="C#" %>

<html>
<body>
<form id="Form1" runat=server>
   <h1>Parent Master</h1>
   <p>
   <font color=red>This is Parent-master content.</font>
   </P>
<asp:contentplaceholder id="MainContent" runat=server/>
</form>
</html>
</body>

// MasterPage5.master (Child Master)
<%@ master language="C#" master="~/MasterMasterPage5.master" %>

<asp:content id="Content1" contentplaceholderid="MainContent" runat="server">
   <asp:panel runat="server" id="panelMain" backcolor="lightyellow">
   <h2>Child master</h2>
      <asp:panel runat="server" id="panel1" backcolor="lightblue">
         <p>This is childmaster content.</p>
         <asp:contentplaceholder id="Content1" runat="server" />
      </asp:panel>
      <asp:panel runat="server" id="panel2" backcolor="pink">
         <p>This is childmaster content.</p>
         <asp:contentplaceholder id="Content2" runat="server" />
      </asp:panel>
   </asp:panel>
</asp:content>

// ContentPage5.aspx
<%@ page language="C#" master="~/MasterPage5.master" %>
<asp:content id="Content1" contentplaceholderid="Content1" runat=server>
   <asp:label runat="server" id="Label1"
        text="Child label1" font-bold=true/>
   <br>
</asp:content>
<asp:content id="Content2" contentplaceholderid="Content2" runat=server>
   <asp:label runat="server" id="Label2"
        text="Child label2" font-bold=true/>
</asp:content>
End example

Team LiB
Previous Section Next Section