Home  >  Article  >  Backend Development  >  What is the order in which Asp.NET page events are loaded?

What is the order in which Asp.NET page events are loaded?

巴扎黑
巴扎黑Original
2017-08-06 09:38:371749browse

This article mainly introduces to you the sequence of event loading in Asp.NET pages. The article introduces it in detail through pictures, texts and sample codes. It has certain reference and learning value for everyone. Friends who need it can follow it below. Come and learn with me.

This article mainly introduces to you the relevant content about the loading sequence of Asp.NET page events, and shares it for your reference and study. I won’t say much below, let’s take a look at the detailed introduction:

Events in ASP.NET master pages and content pages

We know that both master pages and content pages can contain event handlers for controls. For controls, controls in the content page raise events in the content page, and controls in the master page raise events in the master page. Control events are not sent from the content page to the master page, and events from master page controls cannot be handled in the content page, they are only handled within their own events.

The following is the sequence of events after the master page (Master) and the content page (ContentPage) are merged:


Master页面控件 Init 事件。
ContentPage页面控件 Init 事件。
Master页  Init 事件。
ContentPage页 Init 事件。
ContentPage页 Load 事件。
Master页  Load 事件。
ContentPage页面控件 Load 事件。
ContentPage页面 PreRender 事件。
Master页面  PreRender 事件。
Master页面控件 PreRender 事件。
ContentPage页面控件 PreRender 事件。

Master The order of events in layout and content pages is not important to the page developer. However, if you create event handlers that depend on the availability of certain events, it is helpful to understand the order of events in master pages and content pages.

The sequence of page event loading in Asp.Net

1. When a single Page is executed, events will be activated in the following order:


Page.PreInit
Page.Init
Page.InitComplite
Page.PreLoad
Page.Load
Page.LoadComplete
Page.PreRender
Page.PreRenderComplete

2. If the page inherits from another page, such as BasePage:System.Web.UI.Page, add a verification function to BasePage. For example, if you check whether you are logged in, whether you have permissions, etc., and other pages inherit from BasePage, the event activation sequence of BasePage and final Page is:


BasePage.PreInit
Page.PreInit
BasePage.Init
Page.Init
BasePage.InitComplite
Page.InitComplite
BasePage.PreLoad
Page.PreLoad
BasePage.Load
Page.Load
BasePage.LoadComplete
Page.LoadComplete
BasePage.PreRender
Page.PreRender
BasePage.PreRenderComplete
Page.PreRenderComplete

3. If you use MasterPage, the events in MasterPage and the events in ContentPage are activated in the following order:


ContentPage.PreInit
Master.Init
ContentPage.Init
ContentPage.InitComplite
ContentPage.PreLoad
ContentPage.Load
Master.Load
ContentPage.LoadComplete
ContentPage.PreRender
Master.PreRender
ContentPage.PreRenderComplete

It should be noted that there is no PreInit event in Master .

4. If ContentPage inherits BasePage, then the execution order of each event will become:


BasePage.PreInit
ContentPage.PreInit
Master.Init
BasePage.Init
ContentPage.Init
BasePage.InitComplite
ContentPage.InitComplite
BasePage.PreLoad
ContentPage.PreLoad
BasePage.Load
ContentPage.Load
Master.Load
BasePage.LoadComplete
ContentPage.LoadComplete
BasePage.PreRender
ContentPage.PreRender
Master.PreRender
BasePage.PreRenderComplete
ContentPage.PreRenderComplete

only Need to remember: load the inherited page first, and then load itself. If the inherited page has inheritance, load the inheritance of the inherited page first.

Init, Load, PreRender event execution sequence:

1) Init event of the control

2) Init event of the page where the control is located

3) Load event of the page where the control is located

4) Load event of the control

5) PreRender event of the page where the control is located

6) PreRender event of the control

Some insights from personal research: (The following two points can be verified by building a self-made page and rewriting related events)

1. The Init event is raised from the innermost control (such as 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 sequence of the same events between controls is from left to right and top to bottom according to the position of the control on the page.

Note:

1. Remember that the user control is also considered a control in the page;

2. Looking at the user control as a separate special page, it itself and the controls it contains also follow the same rules.

3. If the onload event of the client body object is used in the client program (such as JavaScript), please note that this client event is executed last, that is, after all events on the server side have been executed. Just execute.

4. There are no PreInit and OnComplete events in Master and user-defined controls. Master itself is a user control usercontrol. Usercontrol inherits TemplateControl, and TemplateControl inherits Control. Let us see what can be rewritten in Control. event (partial screenshot), you can also view this official document: https://referencesource.microsoft.com/#System.Web/UI/Control.cs,87dbac93d9749fa2.


Event handler name Occurrence time
Page_Init In Web Form The view state loads the server control and initializes it. This is the first step in the form life cycle
Page_Load Loads the server control on the Page object. Since the view state information is available at this time, you can use code here to change the settings of the space or display text on the page.
Page_PreRender The application will render Page

Page_Unload

Page is unloaded from memory

Page_Error

An unhandled exception occurred

Page_AbortTransaction

Transaction terminated

##Page_CommitTransaction

Transaction Accepted

Page_DataBinding

Place the page on the server space Loaded together with the data source binding

Page_Disposed

The Page object is released from memory. This is the last event in the Page object life cycle

The above is the detailed content of What is the order in which Asp.NET page events are loaded?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn