In each stage of the page lifecycle, the page triggers events that can be processed by running your own code. For control events, you can bind an event handler to an event by declaring attributes (such as onclick) or by using code.
Page also supports automatic event connection, that is, ASP. NET will find methods with specific names and automatically run these methods when a specific event is triggered. If the autoeventwireup attribute of the @ page command is set to true (or if this attribute is not defined because it is true by default), page events are automatically bound to the method using the page_event naming convention, for example, page_load and page_init. For more information about automatic event connection, see ASP. NET web server control event model.
The following table lists the most common page lifecycle events. Actual events are more than listed events. However, they are not used in most page processing solutions. It is mainly used by server controls on ASP. NET web pages to initialize and present them. If you want to write your own ASP. NET Server controls, you need to learn more about these stages. For information on creating custom controls, see develop custom ASP. NET Server controls.
Page events |
TYPICAL USE |
Page_preinit |
· Use the ispostback attribute to determine whether the page is processed for the first time. · Create or recreate a dynamic control. · Dynamically set the master page. · Dynamically set theme attributes. · Read or set the configuration file property value. Note: If the request is a send-back request, the value of the control has not been restored from the view State. If the control attribute is set in this phase, the value may be overwritten in the next phase. |
Page_init |
· Read or initialize control attributes. |
Page_load |
· Read and update control attributes. |
Control events |
Execute application-specific processing: · If the page contains a verification program control, check the isvalid attribute of the page and each verification control before performing any processing. · Process specific events, such as click events of the button control. |
Page_prerender |
· Make the final changes to the page content. |
Page_unload |
The final cleanup may include: · Close the connection between open files and databases. · Complete logging or other request-specific tasks. Note: In the unload phase, the page and its controls are displayed, so no further changes can be made to the response stream. If you try to call a method (such as the response. Write method), this page will cause an exception. |