ASP. NET page execution sequence

Source: Internet
Author: User

 

1. object initialization (OnInit method)

Controls in the page (including the page itself) are initialized for the first time in their initial FORM. Declare your object in the constructor of the background code file on the ASPX page. The page will know the object type and how many such objects need to be created. Once you declare your controls in the constructor, you can access them in any subclass, method, event, or attribute of the constructor. However, if any of your objects are controls specified in the ASPX file, such controls have no attributes. In addition, it is dangerous to access them from code, because it cannot guarantee the order in which these control instances are created (assuming they can all be created ). You can use the OnInit method to reload initialization events.

2. Load view status data (LoadViewState event)

After initialization, the control can only be referenced by ID (no Document Object Model has been created for relative reference ). In the LoadViewState event, the initialized control obtains the first attribute: View status information that was last submitted to the server. The page view status is maintained through ASP. NET. It is used to store information to the server during a round-trip. View status information is saved as a name/Value pair, which contains information of controls such as Text and Value. View information is stored in the value property of the hidden <input> Control and passed in the page request. As you know, this is a huge leap in the old ASP3.0 status maintenance technology. This event can be reloaded using the LoadViewState method, which is often used to customize the data it receives when the control is filled.

3. process the returned data (LoadPostData event)

In the page creation stage, the Form data sent to the server (ASP. NET is called the return data) is processed according to the data requirements of each control. When a Form is submitted on a page, the framework implements the IPostBackDataHandler interface on each data submission control. The page then triggers the LoadPostData event. Through page parsing, it is found that the IPostBackDataHandler interface control is implemented, and the control status is updated with correct return data. ASP. NET updates the correct control by matching the unique identifier of the control, which has the name value pair in the name value set. This is one of the reasons that each control requires a unique identifier on all specific pages. Other steps are completed by the framework to ensure that each identifier is unique in the environment. For example, a custom user control exists in a single page. After the LoadPostData event is triggered, the RaisePostDataChanged event can be executed at any time.

4. Object loading (OnLoad method)

The object obtains the correct Form in the Load event. All objects are first organized in the page DOM (called the control tree in ASP. NET), and can be easily referenced by code or relative positions (crawling the DOM. Then the object can freely access the client property set in HTML, such as width, value, or visibility. When loading, the control logic, such as algorithms, control properties set programmatically, and output strings assembled with StringBuilder are all executed simultaneously. Most of the work is completed at this stage. The Load event can be reloaded by calling OnLoad.

5. Run the RaisePostDataChanged event.

As mentioned above, this occurs after all controls that implement the IPostBackDataHandler interface are correctly uploaded and updated. In this process, each control has a Boolean ID that identifies whether the data of the control is changed or kept original after the previous submission. ASP. NET then searches for the changed identifier of any display control data through the search page and fires RaisePostDataChanged. The RaisePostDataChanged event is triggered only when all controls are updated after the Load event occurs. This ensures that the data of other controls is not manually changed in the RaisePostDataChanged event before the control is updated.

6. Process client return events (RaisePostBackEvent event)

After a server event is triggered due to a data change caused by a returned update, the returned object will be processed in the RaisePostBackEvent event. This type of returned object is usually a control that changes its status and triggers the return (its autopostback is enabled) or a clicked form submission button. A lot of code is executed in this event, because this is an ideal place to control the event-driven logic. To ensure the correctness of the data presented to the browser, the RaisePostBackEvent event is finally triggered after a series of return events. Based on consistency, the control changed in the callback is not updated until the function is executed. That is to say, the data changed by the expected event is always reflected on the results page. RaisePostBackEvent events can be captured through RaisePostBackEvent.

7. Object pre-rendering (OnPreRender event)

Objects that are pre-rendered are the last place to change for objects that can be saved to a view or maintain the view State. This makes the pre-rendering step an ideal position for making the final modification. For example, you can change the control properties or the control tree structure without worrying about object changes caused by database requests or view status updates. After the pre-rendering stage, the object changes are locked and cannot be saved to the page view State. The pre-rendering phase can be implemented by reloading OnPreRender.

8. Save view status (SaveViewState event)

The view status is saved only when all page objects change. The object state data is stored in the hidden <input> object, which is the place where the object state data is ready to be presented to HTML. In the SaveViewState event, the value can be saved to the view State object, but changes to the page control cannot be saved to it. You can perform this step by reloading the SaveViewState.

9. Rendering HTML (Render event)

The Render event starts page creation by assembling HTML for browser output. In the Render event, page call objects Render them as HTML, and then page collection HTML is sent. When the Render event is reloaded, developers can create custom HTML for the browser. At this time, any HTML created on the page has not yet taken effect. The Render method uses the HtmlTextWriter object as a parameter and generates HTML for the browser. The modification can still be made here, but it will only be reflected to the customer (the change is only reflected in the HTML rendering, and the view status cannot be changed ). Render events can be overloaded.

10. Release (Dispose event)

When the HTML of the page is displayed, the object is released. In the Dispose event, you can clear any objects or references constructed during page creation. All processing operations have been executed. You can safely release any existing objects, including Page objects. Dispose can be overloaded.

The specific event sequence is as follows:

① Page_Init () object initialization;

② LoadViewState: load the view status;

③ LoadPostData process the delivery data;

④ Page_Load () page loading;

⑤ RaisePostDataChanged data update and return events;

⑥ RaisePostBackEvent process events returned by the client;

7. Page_PreRender () page preprocessing;

SaveViewState: Save the view status;

⑨ Page_Render () presents Html static pages;

⑩ UnLoad release page;

Summary

Every time we request an ASP. NET page, the process from initialization to release is also executed. By understanding the internal execution process of ASP. NET pages, it is easier and more efficient to write and debug code.

The specific code is as follows:

View plaincopy to clipboardprint?
Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;

Public partial class _ Default: Page
{
Protected void Page_Load (object sender, EventArgs e)
{

}

# Region OnPreInit Step 1
Protected override void OnPreInit (EventArgs e)
{
// Check the IsPostBack attribute to determine whether the page is processed for the first time.
// Create or recreate a dynamic control.
// Dynamically set the master page.
// Set the Theme attribute dynamically.
// Read or set the configuration file property value.
// Note
// If the request is a send-back request, the value of the control has not been restored from the view State. If the control property is set in this phase, its value may be overwritten in the next event.
Base. OnPreInit (e );
}
# Endregion

# Region OnInit Step 2
Protected override void OnInit (EventArgs e)
{
// Triggered after all controls have been initialized and all appearance settings have been applied. Use this event to read or initialize control properties.
Base. OnInit (e );
}
# Endregion

# Region OnInitComplete step 3
Protected override void OnInitComplete (EventArgs e)
{
// Triggered by the Page Object. This event is used to handle all tasks that require initialization to be completed first.
Base. OnInitComplete (e );
}
# Endregion

# Region PreLoad Step 4
Protected override void OnPreLoad (EventArgs e)
{
// Use this event if you need to process pages or controls before loading events.
// After a Page event is triggered, it loads the view status for itself and all controls, and then processes any sending data included in the Request instance.
Base. OnPreLoad (e );
}
# Endregion

# Region OnLoad Step 5
Protected override void OnLoad (EventArgs e)
{
// Page calls the OnLoad event Method on the Page, and then performs the same operation on each sub-control recursively. This repeats until the current Page and all controls are loaded.
// Use the OnLoad event method to set properties in the control and establish a database connection.
Base. OnLoad (e );
}
# Endregion

# Step 6 of region control events
Protected void button#click (object sender, EventArgs e)
{
// Use these events to process specific control events, such as Click events of the Button control or TextChanged events of the TextBox Control.
// Note
// In the send-back request, if the Page contains the verification program control, check the IsValid attribute of the Page and each verification control before performing any processing.

}
# Endregion

# Region OnLoadComplete Step 7
Protected override void OnLoadComplete (EventArgs e)
{
// Use this event for tasks of all other controls on the page to be loaded.
Base. OnLoadComplete (e );
}
# Endregion

# Region OnPreRender Step 8
Protected override void OnPreRender (EventArgs e)
{
// Before the event:
// The Page Object calls EnsureChildControls for each control and Page.
// The DataBind method is called for each data binding control with the performanceid attribute set. For more information, see the data binding event of the data binding control below.
// A PreRender event occurs for each control on the page. Use this event to make the final changes to the page or its control content.
Base. OnPreRender (e );
}
# Endregion

# Region SaveStateComplete Step 9
Protected override void OnSaveStateComplete (EventArgs e)
{
// ViewState is saved for the page and all controls before this event occurs. Any changes made to the page or control at this time will be ignored.
// Use this event to execute tasks that meet the following conditions: the view status is saved, but no changes are made to the control.
Base. OnSaveStateComplete (e );
}
# Endregion

# Region Render step 10
// Render
// This is not an event. At this stage of processing, the Page object calls this method on each control. All ASP. NET Web server controls have a Render method used to write the control flag sent to the browser.
// If you create a custom control, you usually need to override this method to output the control tag. However, if the custom control only merges the standard ASP. NET Web server control and does not merge the custom tag, the Render method is not required. For more information, see develop custom ASP. NET Server controls.
// The user control (. ascx file) is automatically merged and rendered, so you do not need to explicitly present the control in the code.
# Endregion

# Region OnUnload Step 1
Protected override void OnUnload (EventArgs e)
{
// This event first occurs for each control and then for this page. In the control, use this event to perform final cleaning on the specific control, for example, closing the connection to the specific database of the control.

// For the page itself, use this event to perform the final cleaning, for example, closing the opened file and database connection, or completing logging or other request-specific tasks.
// Note
// When the page and its control are displayed, no further changes can be made to the response stream. If you try to call a method (such as the Response. Write method), this page will cause an exception.
Base. OnUnload (e );
}
# Endregion
}

From CSDN blog, reproduced from: http://blog.csdn.net/a497785609/archive/2009/09/02/4510335.aspx

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.