mtelligent

View Original

Exam 70-553 - Implement a consistent page design by using master pages.

Most of this post was written by Juan Suero, another developer who works for Avanade. Check his blog, Meta Dojo to see some of the other interesting things he has to say.

Section 2

  • Part 5
    Customizing and Personalizing a Web Application
    • Topic 1

Implement a consistent page design by using master pages.

  • Create a master page.
  • Add a ContentPlaceHolder control to a master page.
  • Specify default content for a ContentPlaceHolder.
  • Reference external resources in a master page.
  • Define the content of a particular page in a content page.
  • Create a content page.
  • Add content to a content page.
  • Reference a master page member from a content page.
  • Handle events when using master pages.
  • Create a nested master page.
  • Change master pages dynamically.

Summary

Net to .Net 2.0 Master pages contain all the top-level HTML elements for a page, such as <html>, <head>, and <form>. They also define sections with controls called ContentPlaceHolders. ASPX pages use master pages to define layout ensuring a consistent site design. ASPX define mappings between the ContentPlaceHolders of the masterpage and the Content sections of the ASPX page. Master pages have a .master file extension. Masterpages are tied to ASPX pages via the MasterPageFile attribute of the Page directive or in webconfig via the /configuration/system.web/pages MasterPageFile attribute.

Be careful with the term Master.. although it may seem that the masterpage is higher up in the food chain and things filter down from it, The masterpage itself inherits from user control and, at runtime, bocomes a child of the page of which it is a "master" of. There is a method to the madness. There is a new stage in the page lifecycle call PreInit. It is here that themes and masterpages are leveraged to provide modular formatting and structure to the user interface. At this stage the Page object takes the definition for the masterpage (all your ContentPlaceHolders controls) and "superimposes" it onto itself. It then inserts all the controls from each of the Content Control sections of your page into thier respective ContentPlaceHolders. This mapping between ContentPlaceHolders and Content controls is done via the ContenPlaceHolderID.

ContentPlaceHolder Controls are used to define where the content will be inserted into the Master Page Template. You can set the Master Page through Configuration in Web.Config, with a Page Directive Attribute, or dynamically in the PreInit event like the following example:

Dynamically Set a masterpage and Theme in the PreInit Method

void Page_PreInit( Object sender, System. EventArgs e)
{
Page.MasterPageFile = "~/MasterPages/MasterPage.master" ;
Page.Theme = "FunkyTheme" ;
}

After the PreInit method, the Master Page can not be changed.

In a master page, you can define default content inside a ContentPlaceHolder Control so when a page that uses the master page does not specify content for that content placeholder, ASP.Net will use the default content.

In order to nest master pages, just have the master page you are building use a master page on it’s own.

Other Resources & Links:

Master Pages in ASP.NET 2.0
http://msdn.microsoft.com/asp.net/reference/design/default.aspx?pull=/library/en-us/dnvs05/html/masterpages.asp

Creating a Layout Using Master Pages
http://www.asp.net/QuickStart/aspnet/doc/masterpages/default.aspx

Events in ASP.NET Master and Content Pages
http://msdn2.microsoft.com/dct97kc3.aspx