Home >Backend Development >C#.Net Tutorial >What is the order in which Asp.NET page events are loaded?
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.
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!