In-depth understanding of the event execution sequence and page sending back when running the page

Source: Internet
Author: User

Copy codeThe Code is as follows: 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.
// Pay attention to www.2cto.com
// 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
}

When the page is sent back, if you click the button, all the above events will be re-executed. The execution order is as follows:
1. OnPreInit
2. OnInit
3. OnInitComplete
4. OnPreLoad
5. Page_Load
6. OnLoad
7. Button_Click
8. OnLoadComplete
9. OnPreRender
We can see that the Button_Click event is executed after OnLoad. You can test it:Copy codeThe Code is as follows: public partial class TestControls: System. Web. UI. Page
{
Static int count = 0;
Protected void Page_Load (object sender, EventArgs e)
{
Response. Write (count + "Page_Load <br/> ");
Count ++;
}
Protected override void OnPreInit (EventArgs e)
{
Base. OnPreInit (e );
Response. Write (count + "OnPreInit <br/> ");
Count ++;
}
Protected override void OnInit (EventArgs e)
{
Base. OnInit (e );
Response. Write (count + "OnInit <br/> ");
Count ++;
}
Protected override void OnLoad (EventArgs e)
{
Base. OnLoad (e );
Response. Write (count + "OnLoad <br/> ");
Count ++;
}
Protected override void OnPreLoad (EventArgs e)
{
Base. OnPreLoad (e );
Response. Write (count + "OnPreLoad <br/> ");
Count ++;
}
Protected override void OnLoadComplete (EventArgs e)
{
Base. OnLoadComplete (e );
Response. Write (count + "OnLoadComplete <br/> ");
Count ++;
}
Protected override void OnInitComplete (EventArgs e)
{
Base. OnInitComplete (e );
Response. Write (count + "OnInitComplete <br/> ");
Count ++;
}
Protected override void OnUnload (EventArgs e)
{
Base. OnUnload (e );
}
Protected override void OnDataBinding (EventArgs e)
{
Base. OnDataBinding (e );
Response. Write (count + "OnDataBinding <br/> ");
Count ++;
}
Protected override void OnPreRender (EventArgs e)
{
Base. OnPreRender (e );
Response. Write (count + "OnPreRender <br/> ");
Count ++;
}
Protected void btnGraphics_Click (object sender, EventArgs e)
{
// Bitmap bmp = new Bitmap (10, 10 );
// Graphics g = Graphics. FromImage (bmp );
Response. Write (count + "btnGraphics_Click <br/> ");
Count ++;
}
}

1. Familiar with the whole process of the Request pipeline program running:
(1): BeginRequest: Start to process the request
(2) AuthenticateRequest authorization authentication request to obtain user authorization information
(3): PostAuthenticateRequest is obtained successfully.
(4): AunthorizeRequest authorization, generally used to check whether the user has the permission
(5): PostAuthorizeRequest: get authorization
(6): ResolveRequestCache: Get Page cache results
(7): PostResolveRequestCache has obtained the cache.
(8): PostMapRequestHandler creates a Page Object
(9): AcquireRequestState obtains the Session ----- first checks whether the current page object implements the IRequiresSessionState interface. If yes, the SessionID is obtained from the request message style sent by the browser, obtain the corresponding Session object in the Session pool of the server, and assign the Session attribute to HttpContext.
(10) PostAcquireRequestState to get the Session
(11) PreRequestHandlerExecute: Prepare to execute the Page Object
Execute the ProcessRequest method of the Page Object
(12) PostRequestHandlerExecute: The Page Object is executed.
(13) ReleaseRequestState: Release Request status
(14) The PostReleaseRequestState has been released.
(15) UpdateRequestCache update Cache
(16) PostUpdateRequestCache updated Cache
(17) LogRequest Logging
(18) PostLogRequest completed logs
(19) EndRequest complete,Copy codeThe Code is as follows: public class getsession: System. Web. UI. Page, IReadOnlySessionState
{
String ss = "";
Public void Init (HttpApplication context)
{
// You can add various request pipelines as needed.
// Obtain the Session
Context. AcquireRequestState + = new EventHandler (context_AcquireRequestState );
// Obtain the Url
Context. BeginRequest + = new EventHandler (context_BeginRequest );
}
Void context_AcquireRequestState (object sender, EventArgs e)
{
If (Session ["user"]! = Null)
{
Ss = Session ["user"]. ToString ();
}
}
Void context_BeginRequest (object sender, EventArgs e)
{
// Obtain the HttpApplication object of the current page request Pipeline
HttpApplication application = sender as HttpApplication;
HttpContext context = application. Context; // obtain the context object
String url = context. Request. Url. LocalPath; // obtain the URL (excluding the domain name and Path)
}
}

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.