Document directory
- 1. Start
- 2. Initialize
- 3. Load
- 4. Validation
- 5. Event Handling
- 6. Render
- 7. Unload
The article I just saw from codeproject makes sense. I have not posted an urgent translation yet. Let's take a look.
Introduction
For ASP. NET developers, understanding the ASP. NET page lifecycle is important for your reasons, but primarily for knowing where to place particle methods, and when various page properties are set. however, it can often be difficult to remember and make sense out of all of the methods available in the page lifecycle. while there are scores of articles on the internet related to the internal mechanics of the page lifecycle, this article intends to begin by covering the basics and providing a simple and easy understanding of usage.
It can be difficult to remember exactly what happens and when during the ASP. NET page lifecycle. that in mind, often the easiest way to make sense out of a series of steps is to create an acronym. microsoft shows us the basic steps of the ASP. NET page lifecycle below.
- Page Request
- Start
- Page Initialization
- Load
- Validation
- Postback event handling
- Rendering
- Unload
Putting together an acronym for these is easy enough. since the Page Request technically isn't a part of the life cycle (it only indicates whether we actually will start the cycle or load a cached page) we won't include it in the acronym.
- S-Start
- I-Initialize
- L-Load
- V-Validate
- E-Event Handling
- R-Render
That gives us "SILVER", which is very easy to remember. however, it is important remember that the last part of the cycle is unload. you can remember it as "SILVER-U" or "SILVER-YOU" if that helps (but it just didn't quite fit into our acronym !). Now that it's easy to remember the order of steps for the page lifecycle, we'll summarize exactly what happens and what events are pertinent to each stage.
1. Start
This is where page properties suchRequest
,Response
,IsPostBack
AndUICulture
Are set. As a developer, you most likely won't really need to do anything in this stage of the cycle. If you need to access or override behavior for this step, usePreInit
Method to create or re-create dynamic controls, set a master page or theme or read or set profile property values. it is important to note that if the request is a postback, the values of the controls have not yet been restored from view state. if you set a control property at this stage, its value might be overwritten in the next event.
2. Initialize
This stage can be very important to developers. Here, themes are applied, and unique ids are generated and set for controls. Developers have access toInit
,InitComplete
AndPreLoad
Methods in this stage. Microsoft's recommended usage for these methods is as follows:
Init
-This event is raised after all controls have been initialized and any skin settings have been applied. Use this event to read or initialize control properties.
InitComplete
-This event is raised byPage
Object. Use this event for processing tasks that require all initialization be complete.
PreLoad
-Use this event if you need to perform processing on your page or control before the Load event. after the Page raises this event, it loads view state for itself and all controls, and then processes any postback data has ded with the Request instance.
3. Load
This stage is perhaps the most utilized by developers. In this stage controls are loaded with information retrieved from view and control states.OnLoad
Is the event method that fires during this stage. This is where you will want to set properties for all of the server controls on your page, request query strings, and establish database connections.
4. Validation
If you have controls that require validation, they are validated here and you can now check the IsValid property of the control. The event associated with this isValidate
, Which contains one overloaded method that accepts a validation group string. The overloaded method instructs the controls in the specified group to validate.
5. Event Handling
The event handling for server controls occurs during this stage. This means that events suchClick
,SelectedIndexChanged
, Etc are applied to your server controls, and, in the case of a postback, these event handlers are fired by the control. The accessible events of note in this stage are as follows:
LoadComplete
-At this step, all of the controls for the page have been loaded.
PreRender
-A few things of import happen here. First, the page object will callEnsureChildControls
For each control, and finally for the page. Additionally, any data bound control that hasDataSourceID
Set will call itsDataBind
Method. It is important to note thatPreRender
Event occurs for each control on the page. At the conclusion of this event, ViewState will be saved for the page and all of the controls.
SaveStateComplete
-ViewState has been saved. If you have actions that do not require changes to controls but require ViewState to have been saved, you can handle the SaveStateComplete event.
6. Render
Render is not really an event. rather, the page object callthis method on each control, which in turn writes out the HTML markup for the control to the browser. this stage is keenly important to developers who create custom controls, because the standard approach is to overrideRender
Method for the control in order to output the custom markup. If your control inherits from a standard ASP. NET server control, you probably won't need to overrideRender
Method unless you want to exhibit a different behavior than the control's default. this is outside the scope of this document, but for more reading, you can reference Microsoft's Developing asp m asp. NET Server Controls. http://msdn2.microsoft.com/en-us/library/zt27tfhy.aspx)
7. Unload
This final event occurs first for each control, then, finally, for the page. at this point, all controls have been rendered to the output stream and cannot be changed. during this event any attempt to access the response stream will result in an exception being thrown. this event is primarily for cleanup routines such as closing open database connections and open file streams, or, event logging and other tasks.
Methods
The following methods (which can all be overridden) occur in order during the lifecycle of an ASP. NET page. please realize that some of these methods are called recursively, and multiple times depending on the content of the page. this list is the generalized order in which methods fire when a page loads. you can test this by creating a default ASP. NET application, overloading each of the below methods, and setting a breakpoint on each.
Construct
ProcessRequest
FrameworkInitialize
InitializeCulture
- If child controls are present:
AddParsedSubObject
CreateControlCollection
AddedControl
ResolveAdapter
DeterminePostBackMode
OnPreInit
OnInit
TrackViewState
OnInitComplete
OnPreLoad
OnLoad
OnLoadComplete
EnsureChildControls
CreateChildControls
OnPreRender
OnPreRenderComplete
SaveViewState
OnSaveStateComplete
CreateHtmlTextWriter
RenderControl
Render
RenderChildren
VerifyRenderingInServerForm
OnUnload
Dispose
Conclusion
When developing ASP. NET applications, it's important to know what happens when. understanding how events unfold inside of the page will save you several hours of headache and debugging. while the order of the methods may be hard to remember, I hope that applying the anegis will be of considerable use when determining what needs to happen where inside of your application.
I put this article together as much to help other people, as to help myself. even experienced developers can sometimes forget the order in which things occur. this is not intended to be an all-encompassing article. rather, it is my hope that beginning and intermediate developers can carry this "hip pocket" material around to help mitigate at least some of the more common mistakes.
Happy ASP. net' ing!