ASP. NET page event execution sequence is described
- 2020-06-03 06:13:53
- OfStack
#region Control event The first 6 step
protected void Button1_Click(object sender, EventArgs e)
{
// Use these events to handle specific control events, such as Button The control of Click Event or TextBox The control of TextChanged Events.
// Pay attention to
// In a postback request, check if the page contains validator controls before performing any processing Page And each validation control IsValid Properties.
}
#endregion
#region OnLoadComplete The first 7 step
protected override void OnLoadComplete(EventArgs e)
{
// Use this event for tasks that need to load all other controls on the page.
base.OnLoadComplete(e);
}
#endregion
#region OnPreRender The first 8 step
protected override void OnPreRender(EventArgs e)
{
// Prior to the incident:
//Page Object is called for each control and page EnsureChildControls .
// Set up DataSourceID Each data-bound control of a property is called DataBind Methods. For more information, see data binding events for data bound controls below.
// Every control on the page occurs PreRender Events. Use this event to make last changes to the contents of a page or its controls.
base.OnPreRender(e);
}
#endregion
#region SaveStateComplete The first 9 step
protected override void OnSaveStateComplete(EventArgs e)
{
// Saved for the page and all controls before this event occurs ViewState . Any changes made to the page or control at this time are ignored.
// Use this event to perform a task that meets the requirement that view state has been saved but that no changes have been made to the control.
base.OnSaveStateComplete(e);
}
#endregion
#region Render The first 10 step
//Render
// This is not an event; At this stage of processing, Page Object calls this method on each control. all ASP.NET Web Both server controls 1 A control tag that is used to write and send to the browser Render Methods.
// If you create a custom control, you typically override this method to output the tag of the control. However, if the custom control only merges the standard ASP.NET Web Server control, which does not incorporate custom tags, does not need to be overridden Render Methods. For more information, see Developing Customizations ASP.NET Server controls.
// User control ( .ascx File), so you don't need to explicitly render the control in your code.
#endregion
#region OnUnload The first 101 step
protected override void OnUnload(EventArgs e)
{
// This event occurs first for each control and then for the page. In a control, use this event to perform a final cleanup of a specific control, such as closing a control-specific database connection.
// For the page itself, use this event to perform final cleanup, such as closing open files and database connections, or completing logging or other requested specific tasks.
// Pay attention to
// During the unload phase, the page and its controls are rendered, so the response flow cannot be processed 1 Step change. If you try to call a method (such as Response.Write Method, the page will throw an exception.
base.OnUnload(e);
}
#endregion
}
When the page is postback, such as clicking the button, the above events will be executed once again. At this time, the execution order is:
1. OnPreInit
2. OnInit
3. OnInitComplete
4. OnPreLoad
5. Page_Load
6. OnLoad
7. Button_Click
8. OnLoadComplete
9. OnPreRender