When writing asp.net code within a class that derives from System.Web.UI.Page (i.e., when creating a Web Form), it is important to understand the order in which the page's events fire. Being familiar with the page event lifecycle will allow you to make the best decision on when you can (and should) interact with page properties, controls, state, native resources, etc.
During the page lifecycle, events (e.g., Init, PreRender, etc.) are fired that trigger certain built-in functionality to happen. Developers can interact with these events and add their own customizations. There are two common practices for interacting with these page lifecycle events:
- Override the methods that raise these events. This is done by overriding the “On” methods (e.g., OnInit, OnPreRender, etc.) of the page.
- Add event handlers to the page. These event handlers will be called when the page's lifecycle events fire. The first step to making this work is to ensure that the AutoEventWireup="true" attribute exists in the @ Page directive. Next, simply use the Page_eventname syntax for each event that you want to intercept. You can see this in action with the default Page_Load method found on every newly created Web Form. So, the following two code examples would accomplish the same thing, with the second example requiring less typing:
Below I have listed the most common page events in the order they occur and some information on what is going on when a particular event fires.
PreInit
When the PreInit event fires, personalization settings are initialized for the page. This is the appropriate time to dynamically change a page’s theme or master page.
View state is not available yet.
Init
The Init event is responsible for setting up all declared server controls on the page. Because view state is not available yet, each control’s state is set to its default value. There are no guarantees as to the order in which controls will be initialized. Therefore, declared server controls cannot talk to each other at this time.
InitComplete
This event occurs once all controls have been initialized. At this point in the page lifecycle, you can access all declared server controls and dynamically created controls that were created in conjunction with the Init event. You will not be able to see any user inputted data since view state has not populated the controls yet.
PreLoad
At this point, postback and view state data has been loaded for each control on the page.
Load
This is the most well known event because Web Forms automatically create the Page_Load event handler for the Load event. At this point in the page lifecycle, all the controls on the page are loaded and setup with view state and postback data. This is a very common place for controls to be customized based on user input. Native resource calls (e.g., database access, file I/O, etc.) commonly reside in the Page_Load method as well.
Control Events
After the page’s load event has completed, any fired server control events are handled. For example, if a page has a Button control on it and a user presses the button, the event handler for the button press will occur at this point in the page lifecycle.
LoadComplete
All controls have been setup and control events handled at this point.
PreRender
PreRender occurs before the page’s controls are converted to HTML markup and sent out to the requesting browser. During the PreRender event, any controls that have their DataSourceID property set will have their DataBind method called. As an example, if you have a GridView that is data-bound to an ObjectDataSource, this is the point in the page lifecycle that the GridView will actually be filled with the ObjectDataSource’s SelectMethod data.
PreRenderComplete
At this point, pre-rendering has completed and the page is just about to be turned into an IO stream and sent out to the requesting browser. Since all controls that have their DataSourceID property set have now been filled with data, you can use this method to customize these data-bound controls. For example, you could add a custom ListItem to a DropDownList that is data bound to an ObjectDataSource.
SaveStateComplete
Right before this event is fired, all view state is saved for the page. If you try and make any changes to the page or its controls, they will be discarded.
Unload
At this late stage in the page lifecycle, the page is about to be unloaded from memory. This is a good time to do final clean-up operations like closing database connections or closing open files. This is also a great time to post log entries. The response stream is now closed, so you should not try to make calls into Response or you will get an exception.