ASP master pages

From Wikipedia, the free encyclopedia

Master Pages are a new feature of ASP.NET 2.0, which allow for template-based web programming. A common requirement for web sites is that they provide a consistent look and feel between pages - for example, Wikipedia provides the same navigation system on the left edge of the page and along the top, regardless of what article is displayed. Master Pages give the ASP.NET programmer a new way of achieving this.

In the past, developers have used a number of methods to work around the lack of templates in ASP.NET. Because the .NET platform is object oriented and allows for inheritance, many developers would define a new base class that inherits from System.Web.UI.Page, write methods here that render HTML, and then make the pages in their application inherit from this new class. While this allows for common elements to be reused across a site, it adds complexity and mixes code with markup. Furthermore, this method can only be visually tested by running the application - not while designing it. Other developers have used include files and other tricks to avoid having to implement the same navigation and other elements in every page.

A web application can have one or more master pages, in fact they can even be nested. [1] These have special place-holder controls, called ContentPlaceHolders, which will be filled in later, as well as HTML and JavaScript that will be shared across child pages. ( Using Wikipedia as an example, the text of an article would fill a ContentPlaceHolders. )

Child pages use ContentPlaceHolder controls, which must be mapped to the place-holder of the master page that the child is populating. The rest of the page is defined by the shared parts of the master page, much like a mail merge in a word processor. All markup and server controls in the child page must be placed within the ContentPlaceHolder control.

When a request is made for a child page, ASP.NET merges the output of the content page with the output of the master page, resulting in a page that combines the master page layout with the output of the content page.

One complaint against master pages is that the head HTML tag is defined in the master page. This element must be shared across all pages that take their layout from the master. Several options are available to work around this issue, including modifying the head content at Runtime using Server-side code. The title, however, is freely editable via the title attribute in the master page file declaration at the top of a content page. It is actually possible to place a ContentPlaceHolder control inside the head section of the master page, although Visual Studio 2005 will generate warnings. Unfortunately, markup placed inside the associated Content control is rendered as a literal. The HtmlHead object contains specialized parsing routines to recognize elements such as link and script. This parsing code is not applied to children of a child ContentPlaceHolder control. However, there is a workaround for this. If each link and script control uses the runat="server" attribute, then they will be parsed by ContentPlaceHolder as individual HtmlGenericControls. Code can then be written to iterate through each HtmlGenericControl and replace it with the appropriate control type (HtmlLink, HtmlMeta, etc.) after performing attribute mapping.

  1. ^ ASP.NET Master Pages Overview (Microsoft Developer Network)


Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.