📜  ASP.Net生命周期

📅  最后修改于: 2020-12-27 13:20:52             🧑  作者: Mango

ASP.NET页面生命周期

在ASP.NET中,网页的执行生命周期包括各个阶段。这些阶段包括初始化,实例化,还原和维护状态等。需要了解页面生命周期,以便我们可以在任何阶段放置自定义代码来执行业务逻辑。

页面生命周期阶段

下表包含ASP.NET网页的生命周期阶段。

Stage Description
Page request This stage occurs before the lifecycle begins. When a page is requested by the user, ASP.NET parses and compiles that page.
Start In this stage, page properties such as Request and response are set. It also determines the Request type.
Initialization In this stage, each control’s UniqueID property is set. Master page is applied to the page.
Load During this phase, if page request is postback, control properties are loaded with information.
Postback event handling In this stage, event handler is called if page request is postback. After that, the Validate method of all validator controls is called.
Rendering Before rendering, view state is saved for the page and all controls. During the rendering stage, the page calls the Render method for each control, providing a text writer that writes its output to the OutputStream object of the page’s Response property.
Unload At this stage the requested page has been fully rendered and is ready to terminate.at this stage all properties are unloaded and cleanup is performed.

请求的页面在处理之后首先加载到服务器内存中,然后发送到Bowser。最后,将其从服务器内存中卸载。 ASP.NET在页面生命周期的每个阶段提供了可以在应用程序中使用的方法和事件。在下表中,我们是事件表。

ASP.NET生命周期事件

Page Event Typical Use
PreInit This event is raised after the start stage is complete and before the initialization stage.
Init This event occurs after all controls have been initialized.
We can use this event to read or initialize control properties.
InitComplete This event occurs at the end of the page’s initialization stage.
We can use this event to make changes to view state that we want to make sure are persisted after the next postback.
PreLoad This event is occurs before the post back data is loaded in the controls.
Load This event is raised for the page first time and then recursively for all child controls.
Control events This event is used to handle specific control events such as Button control’ Click event.
LoadComplete This event occurs at the end of the event-handling stage.
We can use this event for tasks that require all other controls on the page be loaded.
PreRender This event occurs after the page object has created all controls that are required in order to render the page.
PreRenderComplete This event occurs after each data bound control whose DataSourceID property is set calls its DataBind method.
SaveStateComplete It is raised after view state and control state have been saved for the page and for all controls.
Render This is not an event; instead, at this stage of processing, the Page object calls this method on each control.
Unload This event raised for each control and then for the page.