Customizing and Managing Your Site's Appearance - Part 2 / Page 4

Customizing and Managing Your Site's Appearance - Part 2 [con't]

Defining the Master Page

As already mentioned, a master page looks like a regular Web Form except that it has a different extension and directive. As well, a master page contains one or more ContentPlaceHolder controls. The following example illustrates a sample master page (the positioning and formatting is contained in its style sheet).

Notice that both ContentPlaceHolder controls contain default content. This default content is displayed if the aspx files that use this master page do not provide Content controls for these ContentPlaceHolder controls.

Let us now define a Web Form that uses this master page.

By removing the markup for the common elements and placing them into the master page, you are left with a Web Form that is quite striking in its clarity and conciseness. It contains only the content that is unique to this page. The result, using a style sheet quite similar to that shown in Listing 6.8, is shown in Figure 6.13.

Notice that all content in this page is contained within either of the two Content controls. Because the Web Form uses a master page, ASP.NET does not allow us to place any markup outside of these two controls. If you do place content outside of a Content control, you will see a Parser Error (see Figure 6.14.

Nested Master Pages

Master pages can be nested so that one master page contains another master page as its content. This can be particularly useful for Web sites that are part of a larger system of sites. For instance, the master page shown in Figure 6.13 could be just one intranet in a much larger system. You might thus want a way to move between these intranets. Nested master pages provide this mechanism. Figure 6.15 illustrates how the master page in Figure 6.13 can be a child nested inside another parent master page.

To nest a master page within another master page, the child master page simply needs a Content control that maps to a ContentPlaceHolder control in the parent master page, as well as the appropriate MasterPageFile attribute in the Master directive. For instance, you could modify the previous example of a master page so that it is contained within a Content control, and thus becomes a child master page.

Your parent master page is quite straightforward with just the one ContentPlaceHolder control. Once again, the formatting and positioning is handled by CSS.

Core Note: There is no designer support for nested master pages in Visual Studio.

Customizing and Managing Your Site's Appearance


Printed with permission from from the book Core Internet Application Development with ASP.NET 2.0 written by Randy Connolly. ISBN 0321419502 • Copyright © 2007 Prentice Hall..


Digg This Add to del.icio.us


[previous]

URL: