An in depth understanding of the sequence of events when running the page page and whether the page is postback or not

  • 2020-05-19 04:45:28
  • OfStack

 
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  The first 1 step  
protected override void OnPreInit(EventArgs e) 
{ 
// check  IsPostBack  Property to determine if it is first 1 Secondary processing of the page.  
// Create or recreate a dynamic control.  
// Dynamically set the master page.  
// Dynamic setting  Theme  Properties.  
// Read or set profile property values.  
// Pay attention to  
// If the request is a postback request, the value of the control has not been restored from view state. If a control property is set at this stage, its value may be below 1 The event is overwritten.  
base.OnPreInit(e); 
} 
#endregion 
#region OnInit  The first 2 step  
protected override void OnInit(EventArgs e) 
{ 
// Raised after all controls have been initialized and all appearance Settings have been applied. Use this event to read or initialize controller properties.  
base.OnInit(e); 
} 
#endregion 
#region OnInitComplete  The first 3 step  
protected override void OnInitComplete(EventArgs e) 
{ 
// by  Page  Object is raised. Use this event to handle tasks that require all initialization work to be done first.  
base.OnInitComplete(e); 
} 
#endregion 
#region PreLoad  The first 4 step  
protected override void OnPreLoad(EventArgs e) 
{ 
// If you need to  Load  Use an event to perform processing on a page or control before an event.  
// in  Page  When the event is raised, it loads the view state for itself and all controls, and then handles it  Request  Any postback data that the instance includes.  
base.OnPreLoad(e); 
} 
#endregion 
#region OnLoad  The first 5 step  
protected override void OnLoad(EventArgs e) 
{ 
//Page  in  Page  On the call  OnLoad  Event method, and then recursively do the same for each child control, and so on until the page and all controls are loaded.  
// use  OnLoad  Event method to set properties in the control and establish a database connection.  
base.OnLoad(e); 
} 
#endregion 
#region  Control event   The first 6 step  
protected void Button1_Click(object sender, EventArgs e) 
{ 
// These events are used to handle specific control events, such as  Button  The control of  Click  Event or  TextBox  The control of  TextChanged  Events.  
// Pay attention to  
// If the page contains a validator control in the postback request, check it 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  The object is called for each control and page  EnsureChildControls .  
// Set up  DataSourceID  Each data bound control of the property is called  DataBind  Methods. For more information, see the data-bound events for data-bound controls below.  
// Each control on the page occurs  PreRender  Events. Use this event to make final changes to the content of the page or its controls.  
base.OnPreRender(e); 
} 
#endregion 
#region SaveStateComplete  The first 9 step  
protected override void OnSaveStateComplete(EventArgs e) 
{ 
// Before this event occurs, it is saved for the page and all controls  ViewState . Any changes made to the page or control at this point will be ignored.  
// Use this event to perform tasks that satisfy the requirement that the 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  The object calls this method on each control. all  ASP.NET Web  Server controls are available 1 Is used to write out the control markup sent to the browser  Render  Methods.  
// If you create a custom control, you usually override this method to output the control's markup. However, if the custom control only merges the standard  ASP.NET Web  Server controls that do not merge custom tags do not need to be overridden  Render  Methods. For more information, see developing customizations  ASP.NET  Server control.  
// User control ( .ascx  File) automatically merges rendering, so there is no 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, this event is used to perform a final cleanup on a particular control, such as closing a control-specific database connection.  
// For the page itself, use this event to perform a final cleanup, such as closing open files and database connections, or completing logging or other request-specific tasks.  
// Pay attention to www.ofstack.com 
// During the uninstall phase, the page and its controls have been rendered, so the response flow cannot be entered 1 Step change. If you try to call a method (e.g  Response.Write  Method), the page will throw an exception.  
base.OnUnload(e); 
} 
#endregion 
} 

When the page has a postback, if the button is clicked, the above events will be re-executed once, in the following order:
1. OnPreInit
2. OnInit
3. OnInitComplete
4. OnPreLoad
5. Page_Load
6. OnLoad
7. Button_Click
8. OnLoadComplete
9. OnPreRender
As you can see, the Button_Click event is executed after OnLoad, so you can test 1:
 
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. Be familiar with the whole process of the request pipeline implementation program:
(1) : BeginRequest: start processing the request
(2) : AuthenticateRequest authorization verification request to obtain user authorization information
(3):PostAuthenticateRequest was successful
(4): AunthorizeRequest authorization, 1 to check whether the user has access
(5):PostAuthorizeRequest: authorized
(6):ResolveRequestCache: get page cache results
(7):PostResolveRequestCache has obtained the cache
(8):PostMapRequestHandler creates page objects
(9):AcquireRequestState gets Session-- determine whether the current page object implements IRequiresSessionState interface. If it does, get SessionID from the request text sent by the browser, and get the corresponding Session object from the Session pool of the server. Finally, assign the Session attribute of HttpContext
(10) PostAcquireRequestState Session
(11)PreRequestHandlerExecute: prepare to execute the page object
Execute the ProcessRequest method of the page object
(12)PostRequestHandlerExecute has finished executing the page object
(13)ReleaseRequestState releases the requested state
(14) the request status of PostReleaseRequestState has been released
(15)UpdateRequestCache update cache
(16)PostUpdateRequestCache has updated the cache
(17)LogRequest logging
(18)PostLogRequest has completed the log
(19) EndRequest completion,
 
public class getsession : System.Web.UI.Page, IReadOnlySessionState 
{ 
string ss = ""; 
public void Init(HttpApplication context) 
{ 
// Here you can add individual request pipelines according to your requirements  
// To obtain Session 
context.AcquireRequestState += new EventHandler(context_AcquireRequestState); 
// To obtain 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) 
{ 
// Gets the current page request pipeline HttpApplication object  
HttpApplication application = sender as HttpApplication; 
HttpContext context = application.Context;// Get context object  
string url = context.Request.Url.LocalPath;// To obtain URL( Does not include domain name and path ) 
} 
} 

Related articles: