|
Create Master Pages in ASP.NET 2.0 (Continued)
Avoid Common Errors
All content within a content page must be placed within an <asp:content> control. A content page cannot have any other controls or HTML outside of an <asp:content> control; otherwise, you'll get an error (see Figure 3 for one of the most common errors developers come across when using master pages). Remember, there can be only one <HTML> and <BODY> section within a rendered page, and this was created already in the master page. Therefore, you need to think of a content page like a user control, in that it's a subset of HTML to be rendered within another page (the master page). If you don't provide an <asp:content> control, then the page will be rendered with any (if defined) default content provided within the <asp:contentplaceholder>.
When the content page is requested, ASP.NET 2.0 retrieves the referenced master page, compiles it, and then compiles the content page and places its content within the desired content placeholder. Of course, you can cache both master and content pages to improve performance, just as in previous versions of ASP.NET.
You can also define a master page for a Web application inside the web.config file. You might want to do this to enforce the use of a master page. This sets the master page definition for all ASPX pages within a Web application controlled by the web.config file. Of course, you can override this setting by specifying a master page inside the <%@ Page %> declaration:
<configuration>
<system.web>
<pages master=
"~/MasterPages/BasicMasterPage.master"
/>
</system.web>
</configuration>
ASP.NET 2.0 also allows you to nest master pages. For example, you might want to define the overall styles, headers, and footers with a top-level master, and then allow each department to define its own master that inherits the top-level master. Content pages for each department would then inherit their own department-specific master page.
In the sample project provided, I created nested master pages called Level1MasterPage.master and Level2MasterPage.master. Level1MasterPage is the top-level master page that provides the layout for headers and footers. Level2MasterPage is the master for menus that you can customize per department (see Listing 3 for the ASPX code for both pages).
You create the top-level master just as you did in the earlier example. However, the second-level master is a little different. Remember, the top-level master contains the initial <HTML> and <BODY> definitions, so you cannot duplicate them within any submasters that inherit the top-level master. Just as the content page can contain only content with <asp:content> tags, the same thing applies to sublevel master pages. In this example, the Level2MasterPage contains an <asp:content> control. Inside this control is the definition for the menu system, and another <asp:contentplaceholder> area for content pages that inherit it. The ContentPlaceHolderID property is linked to the ID of the placeholder defined within the Level1MasterPage.
One of the drawbacks of using nested master pages is that you currently cannot use the VS.NET 2005 Designer view to work with them. You need to work in Source view if you want to work with nested master pages, or content pages that reference nested master pages. Hopefully, this feature will be added by the time VS.NET 2005 is released to production.
A content page can also reference and set values within the master page through the use of the new Master object. This gives you the capability to query or override the master page's properties. Or, you can use this capability to load a master page dynamically to give users the ability to customize their own look and feel based on a set of available master pages (or templates). Let's say you want to add metadata to the HTML header of the current content page—you can add this code to the Page_Load event handler of the content page:
Master.Page.Header.Metadata.Add( _
"Description", _
"Sample master/content page")
You can also access ASP.NET controls on a master page by accessing properties within the new Master object through one of two methods. The Master object gives you access to information about the master page, including the Page object inside the master page itself.
Back to top
|