Jump to content

ASP.NET master pages

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Jimmytharpe (talk | contribs) at 14:27, 19 January 2007 (→‎Criticism: updated to include options for working around criticism). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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.

Master Pages Architecture

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.

Criticism

One complaint against master pages is that, because the head html tag is defined in the master page, so is the title. 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.

References