<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

When is the Master Page Really the Master?

by Tim 24. September 2010 10:28

After a content page's PreInit event has fired, the master page finds the Content control that matches a ContentPlaceHolder and moves it into it's own ContentPlaceHolder - this happens before the content page's Init event. When this process takes place, the master page is truly the master, but by the time the Init event has fired the master page has become a child.

It makes sense then that a master page's Page_Load will occur after the content page's Page_Load, but the master page's Page_Init will occur before the content page's Page_Init event handler. This is because the initialization event works from the inside out - because the master page is inside the content page, the master page's Init event handler will fire before the content page's Init event handler. In this respect, the Init event is an exception to the rule of a page's cycle of events. Here's a sequence in which events occur when a master page is merged with a content page:

  1. Content page PreInit event.
  2. Master page controls Init event.
  3. Content controls Init event.
  4. Master page Init event.
  5. Content page Init event.
  6. Content page Load event.
  7. Master page Load event.
  8. Master page controls Load event.
  9. Content page controls Load event.
  10. Content page PreRender event.
  11. Master page PreRender event.
  12. Master page controls PreRender event.
  13. Content page controls PreRender event.
  14. Master page controls Unload event.
  15. Content page controls Unload event.
  16. Master page Unload event.
  17. Content page Unload event.

If you want to dynamically load a master page at run-time then (as can be seen from the list above) it would have to be done during the page's PreInit event. If you attempt to load the page at a later event, such as the Page_Load event, then you'll receive an exception. The master page must be established so that the page can create an instance of it before any further initialization can occur.

ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps
ASP.NET Page Life Cycle Overview

Comments are closed