How to Use Master Pages (Conclusion) - How to create nested master pages
(Page 6 of 6 )
Unfortunately, Visual Studio 2005 doesn’t support nested master pages in Design view. Microsoft has indicated that nested master pages may be supported in design view in a future version of Visual Studio. But until then, you’ll have to work in Source view to create nested master pages and the content pages that use them. This is described in figure 3-11.
Note that no special coding is required to create a parent master page. As a result, the MasterPage.master page that was shown earlier in this chapter will work fine as a parent master page.
To create a child master page, first use the Add New Item command to add a new item to the web site and choose Master Page as the template for the new item. Then, in Source view, delete all the generated code except for the Master directive and the ContentPlaceHolder element. Change the ID attribute of the ContentPlaceHolder element as appropriate. In this figure, the ID of this element is set to Project.
Next, add a MasterPageFile attribute to the Master page directive. The value of the MasterPageFile attribute should be the name of the page you want to use as the parent master page.
Finally, add a Content element to the master page so that it contains the ContentPlaceHolder element. The ContentPlaceHolderID attribute of the Content element should name the ContentPlaceHolder element in the parent master page. In this example, the ContentPlaceHolderID attribute specifies Main as the name of the content placeholder. If you look back to figure 3-4, you’ll see that Main is the name of the content placeholder in MasterPage.master.
Once you’ve created a child master page, you can create a content page as shown in the second code example in this figure. Here, the MasterPageFile attribute for the Page directive specifies the name of the child master page (ProjectsMaster.master). In addition, the ContentPlaceHolderID attribute of the Content element specifies Project, which matches the ID attribute of the ContentPlaceHolder element in ProjectsMaster.master.
Frankly, the lack of Design view support for nested master pages is a major impediment to their use. So if your application requires nested master pages, be prepared to do most of your development work without the benefit of Design view.
A child master page (ProjectsMaster.aspx) (figure 3-11)
<%@ Master Language="VB" CodeFile="ProjectsMaster.master.vb" Inherits="ProjectsMaster" MasterPageFile="~/MasterPage.master"%>
<asp:Content runat=server ContentPlaceHolderID=Main>
<table border=0 cellpadding=2 cellspacing=0>
<tr>
<td height=40 bgcolor=gainsboro>
<span style="font-size: 24pt">Do-It-Yourself Projects</span>
</td>
</tr>
<tr>
<td valign=top>
<asp:contentplaceholder id="Project" runat="server">
</asp:contentplaceholder>
</td>
</tr>
</table>
</asp:Content>
A content page that uses a child master (Tombstone.aspx)
<%@ Page Language="VB" MasterPageFile="~/ProjectsMaster.master" AutoEventWireup="false" CodeFile="Tombstone.aspx.vb" Inherits="Tombstone" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Project" Runat="server"><br /> Here's how to create your own custom tombstones to give your front-yard cemetery a spooky atmosphere!<br /><br />
Materials needed:<br /><br />
- Sheet of 2" insulation foam<br />
- Gray latex paint<br />
- Stone finish spray paint<br />
- Epitath printed from computer<br />
- Tape<br /><br />
Steps:<br /><br />
- Print the epitath you want to use on normal computer paper.<br />
- Use a Sabre Saw to cut out a piece of insulation foam the size you want your tombstone to be.<br />
- Tape the printed epitath to the face of the tombstone.<br />
- Use a rotary tool to carve the lettering. Carve right through the paper.<br />
- Paint the tombstone with the gray latex paint.<br />
- Spray the tombstone with the stone finish paint.
</asp:Content>
Description
- Because the Web Forms Designer doesn’t support nested master pages, you have to work in Source view to create child masters and content pages that use a child master.
- To create a child master page, add a MasterPageFile attribute to the Master page directive of a master page. Then, delete the rest of the generated code except for the ContentPlaceHolder element, and create a Content element that includes the ContentPlaceHolder element.
- To create a content page that uses a child master, specify the child master in the content page’s MasterPageFile element. Then, add a Content element with the content for the page, and set the ContentPlaceHolderID attribute to the ID of the child master’s content placeholder.
Perspective
I hope this chapter has illustrated the power of master pages. In fact, I recommend that you use master pages for all but the simplest applications, even if you start out with nothing in your master pages but placeholders. Then, when you’re ready to provide a professional look to your content pages, you can enhance the master pages, which will also enhance all of your content pages.
The alternative is to convert regular content pages so they use the master pages that you develop later on. But as figure 3-6 shows, that’s a time-consuming and error-prone procedure. How much better it is to think ahead.
New terms
master page
content page
content placeholder
nested master pages
parent master
child master
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter three of the book Murach’s ASP.NET 2.0 Upgrader’s Guide: VB Edition, written by Anne Boehm and Joel Murach (Murach, 2005; ISBN: 1-890774-36-7). Check it out today at your favorite bookstore. Buy this book now.
|
|