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.
[edit] 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.
[edit] Criticism
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.