For the developer of the ASP. NET WebForm, it is important to understand the page life cycle of ASP. WebForm. The main purpose is to figure out where to place specific methods and when to set various page properties. But remembering and understanding the event-handling methods that are available in the page life cycle is difficult, even if you remember them for a while, but you may forget them for a while. There are many articles about the internal mechanism of the page life cycle, so this article is only prepared to cover the basic parts of the technology, the main purpose is to provide you with a simple way to remember the page life cycle. Accurate memory what happens at each stage of the ASP. NET page life cycle is difficult, and an easy way to remember is to combine an abbreviation based on the name of each stage. Microsoft's documentation gives the following ASP. NET Life cycle:
Page Request
Start
Page initialization
Load
Validation
Postback Event Handling
Rendering
Unload
It is easy to make an abbreviation based on this combination. Since page request is not technically part of the page life cycle (this stage simply indicates whether we are starting a page cycle or loading a page from the cache), we do not include this phase for convenience.
S–start
I–initialize
L–load
V–validate
E–event Handling
R–render
This will combine an abbreviation "SILVER", the English word is very good to remember. Of course, be sure to remember that the last link in the page life cycle unload not included. If you feel the need, you can remember as "silver-u" or "silver-you", although a little damage to the memory of the perfection of the law. Now, it's very easy to remember the page life cycle, and then we summarize what happens at each step and what happens with it.
1. Start
At this stage, page properties such as request, Response, IsPostBack, and UICulture are created. Most of the time, you don't need to do anything at this stage. If you need to invoke or override the behavior of this phase, you can use the PreInit method to create or recreate the dynamic control, set the master page or theme, or read and set the value of the profile. One thing to note is that if it is a postback (postback) page request, the value of all controls has not yet been restored from view state, and if you set the value of a control at this stage, the value may be overridden and overwritten at the following stage.
2. Initialize
This phase is important for developers. At this stage, theme is applied, and all controls are set with a unique ID. At this stage, the developer can invoke the Init, InitComplete, and preload methods. Microsoft's recommendations for using these methods are as follows:
init– This event occurs after all controls are initialized and skin settings are applied. Use this event to read the initialization value of the control.
initcomplete– This event is triggered by the Page object, which is used to handle things that require all initialization work to be done.
Preload-Use this event if you have anything to handle before the page or control enters the Load event. When the page triggers this event, the page loads the view state for itself and all the controls and processes the postback data in all the request.
3. Load
This stage is probably the most used one for developers. At this stage, all controls are populated with information from the ViewState and loaded, and the onload event is triggered. At this stage you can set properties for all server-side controls on the page, get query strings, and establish a database connection.
4. Validation
If your control requires validation, validation occurs at this stage, and you can check the IsValid property of the control at this time. The event associated with this phase is validate, which has an overloaded method that can accept the validation string group (overload method), which performs validation of a particular control group.
5. Event Handling
Event handling for all server-side controls occurs at this stage. That is, click, SelectedIndexChanged, and so on. These events are applied to your server-side controls, and if a page request is a postback (postback), the handler for these events is triggered by the control. The following events can be used at this stage:
loadcomplete– at this stage, all the controls on the page are loaded.
prerender– Here are a few key points, first: the Page object will call each control's EnsureChildControls function and eventually call its own. Second: All data-bound controls that have DataSourceID call their own DataBind functions. One thing to note is that the PreRender event occurs on every control in a page. At the end of this event, the viewstate of the page and all controls are stored.
savestatecomplete– here, ViewState has been stored, if you have any action do not need to modify the control but need to modify the viewstate, you can put in the savestatecomplete inside.
6. Render
Rendering (render) is not actually an event, the Page object calls each control's Render method to output the control's HTML code sequentially. Developers writing user-defined controls are most interested in this phase because the standard way to output user-defined HTML code is to override the Render method. If your control is inherited from an ASP. NET Server-side control, you may not need to override the Render method unless you want to render a behavior that differs from the default behavior of the user control. These are beyond the scope of this document, and if you want to learn more, refer to Microsoft's developing Custom ASP. NET Server Controls. (http://msdn2.microsoft./zt27com/en-us/librarytfhy.aspx)
7. Unload
Finally, the event is triggered by each control, and finally by the page. At this point, all the controls have been rendered as output streams and cannot be modified. In this phase, any attempt to response stream will throw an exception. This event is primarily used for cleanup tasks such as closing database connections and open files, or registering event logs, and so on.
What are the methods in the page cycle
The following is a list of all the methods in the ASP. NET page life cycle, which can be overridden (override), to note that some of these methods will be called recursively, and one will be called repeatedly by the contents of the page, which is arranged in the most General order when the page is loaded.
Construct
ProcessRequest
FrameworkInitialize
InitializeCulture
If child controls is 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 an ASP, it is important to know when something is going on. Understand how events in a page can be layered to save a lot of scratching and troubleshooting time. When the events in these page cycles are difficult to remember, I hope the use of this approach will help you sort out what needs to be done in the program.
Original link: http://www.lanhusoft.com/Article/85.html
(go) teach you to remember the life cycle of the ASP. NET WebForm page