spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / asp / site_appearance2
[previous] [next]

Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?


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

Master Pages

ASP.NET 2.0 provided a treasure trove of new features. Perhaps none of these generated as much anticipation as master pages. This new feature allows the developer to define the structural layout for multiple Web Forms in a separate file and then apply this layout across multiple Web Forms. You can thus move common layout elements, such as logos, navigation systems, search boxes, login areas, and footers, out of all the individual pages and into a single master page.

Any developer who has had to create and maintain an ASP.NET Web application that consists of more than two or three pages should be able to see the value of this ability. In most Web applications, the individual pages typically share a common structure or a common look and feel across the entire site. For instance, most pages within a site may have a logo in the upper-left corner, a global navigation system across the top, a secondary navigation system down the left side of the page, and a footer at the bottom of the page. It is clearly less than ideal to replicate the markup and code for this common structure across multiple pages.

Master pages provide a solution to this problem. They allow the developer to create a consistent page structure or layout without duplicating code or markup. Master pages are created in much the same way as any other Web Form. They contain markup, server controls, and can have a code-behind class that responds to all the usual page lifecycle events. However, they do have their own unique extension (.master) as well as a different directive at the top of the page. As well (and most importantly), they also contain one or more ContentPlaceHolder controls.

The ContentPlaceHolder control defines a region of the master page, which is replaced by content from the page that is using this master page. That is, each Web Form that uses the master page only needs to define the content unique to it within the Content controls that correspond to the ContentPlaceHolder controls in the master page, as illustrated in Figure 6.9.

Like any Web server control, each ContentPlaceHolder control within a master page must have a unique Id.

This Id is used to link the Content controls in the various Web Forms that use the master page. This link is made via the ContentPlaceHolderId property of the Content control.

A master page significantly simplifies the markup for pages that use it. The master page contains the complete XHTML document structure. That is, the html, head, and body elements are contained only within the master page. The.aspx files that use the master page only need define the content that will be inserted into the placeholders in the master page. In fact, these .aspx files can only contain content within Content controls. Unlike HTML frames (which perform a somewhat similar function in HTML), master pages are transparent to the user (that is, the user is unaware of their existence) because ASP.NET merges the content of the master page with the content of the requested page.

Visual Studio and its designer completely support master pages. You can visually edit the master page, as well as visually edit the individual aspx pages as they would appear within the master page. Figure 6.10 illustrates the BookHome.aspx page within the Visual Studio designer. Only the contents of the Content control are editable; the master page markup is displayed (ghosted out) but cannot be edited.

You can specify the master page to use when you add a new Web Form in Visual Studio simply by turning on the Select Master Page option in the Add New Item dialog box (see Figure 6.11).

When you choose this Select Master Page option, Visual Studio then displays the Select A Master Page dialog (see Figure 6.12 . This dialog displays all the master pages in the Web site (i.e., all the files with the .master extension).

Of course, you can use a master page in any Web Form without the intervention of Visual Studio simply by adding the appropriate MasterPageFile attribute to the form's Page directive, as shown in the following.

This example also contains the Title attribute. Because the master page must define the <head> section, you need some way to specify the page title (which is displayed in the browser's title bar) in the individual pages themselves. The Title directive provides one way to do this; the other is to programmatically set the page's Title property. If you do not set it, the page is displayed in the browser with "Untitled" in the title bar.

As an alternative to specifying the MasterPageFile attribute on each Web Form in your site, you can specify it at once globally for all pages in the site via the Pages element in the Web.config file.

Note that setting the MasterPageFile attribute in the Web Form overrides the setting in the Web.config file.

home / programming / asp / site_appearance2
[previous] [next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business

URL: