Monday, April 6, 2015

ASP.Net Page Life Cycle Events when using Nested Master Pages, Content Pages, and Base Pages

A while back I posted Events in ASP.NET Master and Content Pages, regarding the sequence in which events occur when a master page is merged with a content page.

In some cases, the same event is raised in both the content and the master page. For example, both pages raise Init and Load events. The general rule for how events are raised is that the initialization events are raised from the innermost control to the outermost one, and all other events are raised from the outermost control to the innermost one. It is helpful to remember that the master page is merged into the content page and treated as a control in the content page.

I often refer back to that original post as a quick reality check when considering placement for certain code to make sure it happens at the appropriate place in the page life cycle.

But typically my project setup is slightly more complex, and is not just a master page and content page, but more along the lines of:

- Base Master Page (general, script references, style references, etc)
- Nested Master Pages (design/layout/theme)
- Content Pages (page specific functionality)
- Base Page (inherited by each content page typically)

The same rules apply in regards to the sequence of events; however, thinking about this visually can be a little more difficult, so here's a list to help keep it straight:

  • BasePage PreInit
  • Page PreInit
  • Master Init
  • Nested Master Init
  • BasePage Init
  • Page Init
  • BasePage InitComplete
  • BasePage PreLoad
  • BasePage Load
  • Page Load
  • Nested Master Load
  • Master Load
  • BasePage LoadComplete
  • BasePage PreRender
  • Page PreRender
  • Nested Master PreRender
  • Master PreRender
  • BasePage PreRenderComplete
  • BasePage SaveStateComplete
  • Master Unload
  • Nested Master Unload
  • BasePage Unload
  • Page Unload

1 comment:

  1. The aspnet life cycle event are very much useful and helpful My sincere thanks for sharing this post and please countinue to share this kind of post
    Dot Net Training in Chennai