Saturday, November 7, 2009

ASP.NET Page Lifecycle events

From my ASP.NET experience, understanding the Page Lifecycle and what occurs at each point is one of the most confusing (and important) aspects of ASP.NET development.

Here are the 2 MSDN pages that I've gone over several times and memorized:

There is also several pages in my Microsoft Press book that are very similar to these MSDN pages.

The challenge is remembering each of the events, and what is appropriate to do at that particular event. For example:

  • PreInit - Set Master Page, Set Theme, create dynamic controls
  • Init - Controls have been initialized, theme skins applied. Initialize control properties.
  • InitComplete - Perform tasks that require initialization has been completed.
  • PreLoad - Perform any processing that should occur before Load.
  • Load - Set properties in controls and establish database connections
  • Control Events (calling of delegates based on events raised, should check Page.IsValid if there are any Validator controls).
  • LoadComplete - Perform tasks that require load has been completed.
  • PreRender - Prior to this event occurring, Page calls EnsureChildControls for all controls, including itself. Also, all data bound controls have DataBind method called.
  • SaveStateComplete - ViewState has been encoded and saved, then this event occurs.
  • Render - Page object calls Render on every control.
  • Unload - event occurs for each Control, finally for the Page. Do cleanup, close database connections.
*sigh* I like ASP.NET, but I've tended to only use a few of these Lifecycle events (PreInit and Load) to keep things simple. They are SO granular, as you can see from the list above, and it's difficult to remember what-goes-where.

No comments:

Post a Comment