了解ASP.NET的生命周期

Learn the ASP.NET Page Life Cycle

Why learn the various stages of an ASP.NET page? Well if you are a control developer, you have to know the various stages of a page so you can code for the appropriate stage of a page. For example, if you want to perform validation checks on your custom control, you won’t get the desired results if you try and perform validation during the rendering stage!

If you are coming from a ASP.NET 1.1 background, be sure to note that ASP.NET 2.0’s page life cycle has been modified by giving you a finer control of the various stages.

The following are the various stages a page goes through:

Page properties like the Request and Response are set. The type of request is determined, specifically whether it is a new Request or it is a PostBack. Culturing properties are also determined via the pages ICulture property.

All the controls on the given page are initialized with a UniqueID (please don’t confused this with the ID property as the UnqiueID is a unique, hierarchicial identifier which includes the server control’s naming container). Theming is also applied at this stage.

If the current request is a PostBack, the data from the viewstate and control state is loaded to the appropriate properties of the controls.

The Validate method of all the validator controls are fired, which in turn sets the Boolean property IsValid of the controls.

If the current request is a PostBack, all event handlers are called.

ViewState data is saved for all the controls that have enabled viewstate. The Render method for all the controls is fired which writes its output to the OutputStream via a text writer.

Once the page has been rendered and sent, the Page’s properties are unloaded (cleanup time).

So know you have a better understanding of the various stages of a ASP.NET pages life cycle, you should be aware that within each of the above stages there are events that you can hook into so that your code is fired at the exact time that you want it to.

Event Wire-up is another important concept to understand. So Event wire-up is where ASP.NET looks for methods that match a naming convention (e.g. Page_Load, Page_Init, etc) , and these methods are automatically fired at the appropriate event. There is a page level attribute AutoEventWireup that can be set to either true or false to enable this behaviour.

Below are some of the more popular events that you should understand as you will most likely be interested in them:

# You can use this event to: Create /recreate dynamic controls

# Set a master page or theme dynamically

# Access profile properties

Used to read or initialize your control properties.

Used when you need to access your properties after they have been initialized.

Used when you need to process things before the Load event has been fired.

The Page’s OnLoad method and all of its child control’s OnLoad method are fired recursively. This event is used to set properties and make database connections

Control specific events are handled at this stage. e.g. Click event’s for the button control.

This stage is for when you need to access controls that have been properly loaded.

Can be used to make any ‘last chance’ changes to the controls before they are rendered.

This event is used for things that require view state to be saved, yet not making any changes to the controls themselves.

The page object calls this method on all of the controls, so all the controls are written and sent to the browser.

All the controls UnLoad method are fired, followed by the pages UnLoad event (bottom-up). This stage is used for closing database connections, closing open files, etc.

It is import to understand that each server control has its very own life cycle, but they are fired recursively so things may not occur at the time you think they do (they occur in reverse order!). What this means is that some events fire from the bottom up like the Init event, while others load from the top-down like the Load event.