This article mainly introduces to you about the sequence of event loading in the ASP, the text and the sample code introduced in very detailed, for everyone has a certain reference learning value, the need for friends below with the small series to learn together.
This article mainly introduces to you about the ASP page event loading sequence of related content, share out for everyone to reference the study, the following words do not say, come together to see the detailed introduction:
Events in ASP. NET Master pages and content pages
We know that both the master page and the content page can contain event handlers for the control. Controls in the content page raise events in the content page for the control, and controls in the master page raise events in the master page. Control events are not sent from content pages to master pages, and events from master page controls are not processed in content pages, they are processed only within their own events.
The following is the order in which events occur after the master page (master) and content pages (Contentpage) are merged:
Master page Control Init event. Contentpage the page control Init event. Master Page Init event. Contentpage page Init event. Contentpage page Load event. The master page Load event. The Contentpage page Control Load event. Contentpage page PreRender event. Master Page PreRender event. The master page Control PreRender event. Contentpage the page control PreRender event.
The order of events in master pages and content pages is not important for page developers. However, if you create an event handler that depends on the availability of certain events, it is helpful to understand the order of events in the master page and content pages.
Sequence of page event loads in ASP.
1. In a single page execution, the event will be activated in the following order:
Page.PreInitPage.InitPage.InitComplitePage.PreLoadPage.LoadPage.LoadCompletePage.PreRenderPage.PreRenderComplete
2, if the page inherits from another page, for example BasePage:System.Web.UI.Page
, add the verification function in BasePage, such as whether to log in, have permission and so on, while other pages inherit from BasePage, then the event activation order of BasePage and final page is:
BasePage.PreInitPage.PreInitBasePage.InitPage.InitBasePage.InitComplitePage.InitCompliteBasePage.PreLoadPage.PreLoadBaseP Age. LoadPage.LoadBasePage.LoadCompletePage.LoadCompleteBasePage.PreRenderPage.PreRenderBasePage.PreRenderCompletePage.PreRend Ercomplete
3. If masterpage is used, events in MasterPage and events in Contentpage are activated in the following order:
ContentPage.PreInitMaster.InitContentPage.InitContentPage.InitCompliteContentPage.PreLoadContentPage.LoadMaster.LoadConte NtPage.LoadCompleteContentPage.PreRenderMaster.PreRenderContentPage.PreRenderComplete
It is important to note that there is no PreInit event in master.
4. If Contentpage inherits BasePage, then the order of execution of each event will become:
BasePage.PreInitContentPage.PreInitMaster.InitBasePage.InitContentPage.InitBasePage.InitCompliteContentPage.InitCompliteB AsePage.PreLoadContentPage.PreLoadBasePage.LoadContentPage.LoadMaster.LoadBasePage.LoadCompleteContentPage.LoadCompleteBa SePage.PreRenderContentPage.PreRenderMaster.PreRenderBasePage.PreRenderCompleteContentPage.PreRenderComplete
Just remember: Load the inheritance page first, then load yourself, and load the inheritance of the inherited page first if the inheritance page has inheritance.
Event handler Name |
Time of Occurrence |
Page_Init |
Loads the server control and initializes it in the view state of the Web form. This is the first step in the form life cycle |
Page_Load |
The Page object is uploaded into the server control. Because the view state information is available at this time, it is possible to use code to change the settings of the space or to display text on the page. |
Page_PreRender |
The application will render the page |
Page_Unload |
Pages are unloaded from memory |
Page_Error |
An unhandled exception occurred |
Page_aborttransaction |
Transaction is terminated |
Page_committransaction |
Transaction processing is accepted |
Page_databinding |
Load the server space and data source bindings on the page |
Page_disposed |
The Page object is freed from memory. This is the last event in the Page object life cycle |
Init,load,prerender Event Execution Order:
1) The Init event for the control
2) Init event on the page where the control is located
3) Load event on the page where the control resides
4) The Load event for the control
5) PreRender event on the page where the control is located
6) PreRender event for the control
Some of the personal research experience: (The following two points can be verified by self-built page and overwrite related events)
1.Init events are raised from the innermost control (such as a user control) to the outermost control (page), and other events, such as load and PreRender, are raised from the outermost control to the innermost control.
2. The execution order of the same events between controls is performed from left to right, top to bottom, according to the control's position on the page.
Attention:
1. Remember that the user control is also considered a control in the page;
2. Consider the user control as a separate special page, which itself and the controls it contains follow the same rules.
3. If the onload event of the client body pair is used in a client program (such as JavaScript), note that this client event is last executed, that is, after all events on the server side have been executed.
4. Master, user-defined control there is no PreInit, OnComplete event. Master itself is a user control usercontrol,usercontrol inherit TemplateControl, TemplateControl inherit control, let's take a look at the events that can be overridden in control (part), or you can view this official document: https://referencesource.microsoft.com/#System. Web /ui/control.cs,87dbac93d9749fa2.