Detail ASP. NET page lifecycle events

  • 2020-06-15 08:06:29
  • OfStack

Here's how the ASP. NET page started:
1. Page_Init();
2. Load ViewState;
3. Load Postback data;
4. Page_Load();
5. Handle control events;
6. Page_PreRender();
7. Page_Render();
8. Unload event;
9. Dispose method called;

Some of these processes are described below:
1. Page_Init();
This process is mainly the initialization of the controller, each page load to perform this initial process, including the first and later Postback(Postback here, can be simply understood as the user click the SUBMIT button, etc., to the form < Form > Submit to the server, this is postback once), in which you can access the control, but the control value here is not the value we expect in the control, it is only the initial value of 1 control (default value), for example: 1 TextBox1, for example, we fill in the "ha ha", after click SUBMIT submitted page in Page_Init (), and our access to TextBox1. Text is not our "ha ha", but a beginning "of" the empty string if TextBox1 when we design provides a default value, access to here is also provide default values, why, this is about to see a process.

The corresponding event Page.Init

2. Load ViewState
This process loads VIEWSTATE and Postback data, such as TextBox1 above, and assigns "ha", so it is meaningless to assign values to the control in Post_Init(), it will be overwritten during this process, except for the first page load, because there is no VIEWSTATE data.

There is no corresponding event

3.Load Postback data;
Said the above, Postback understandable as the user submits the form data, so here is the processing form data, of course, that is to design to the design of the control, 1 case we can not deal with the process, we'll skip. (before the article about ASP. NET page life cycle of a simple description of the process and Load ViewState in 1 case, it is Microsoft's lifecycle process, separate mentioned here is to make everyone understand that this is a separate process)

There is no corresponding event
4. Page_Load();
This process is also performed every time the page is loaded, but note the difference with Page_Init, which is covered above, note that 1 usually USES ES72en.IsPostBack, which indicates whether the page is being loaded in response to a client postback, or whether it is being loaded and accessed for the first time.


private void Page_Load(object sender, System.EventArgs e)
{
  if(!Page.IsPostBack)
  {
    // The first 1 Time to perform CODE HERE
  }
  else
  {
    // The user submits FORM( namely Postback)CODE HERE
  }
  // Every time I do this, I'm going to execute it CODE HERE
}

Corresponding event Page. Load

5. Handle control events;
During this process, corresponding specific control events, such as private void ListBox1_SelectedIndexChanged(object sender, ES89en.EventArgs e) events, etc

There is no corresponding event (our own event functions are included in this process such as ListBox1_SelectedIndexChanged above)

6. Page_
Rendered object in advance, here is the reciprocal of present data in the user program step 2, I estimate that provides the significance of this process, which is here to control properties and so on should be presented to the user's data is modified, this also is the final modification, the previous changes (such as in Page_Init) can be covered. Finished this also will be a state of operation is to preserve, namely SaveViewState.

The corresponding event is Page.PreRender

7. Page_Render();
You can find it in your browser > Source sees that there is 1 hidden per page < input > ___, "ES117en" will be the page status message written back to our server. Before that, the server has to render the page (that is, construct the file in HTML format), Is from this "__VIEWSTATE" acquired data, of course you also noticed that there's a Page. Render events, we can add your own code, which means we can change the data again, but here recommended not to modify here, now that provides PreRender, should be in the final changes, of course this is not required, just recommended!

The corresponding event, ES125en.Render

8. Unload event;
You should know that when want to ask for an object server will generate one inherit the page objects in memory, namely page classes, it inherits from System. Web. UI. Page.
This event is triggered when a page object is unloaded from memory.

The corresponding event Page.Unload

9. Dispose method called;
Destroy all objects. Occurs when Page is freed from memory. This is the last phase of the lifetime. Maybe Numbers 8 and 9 are a little vague, but I don't know much about them.

The corresponding event Dispose

That is the description of the ASP.NET page cycle.

Note the text on the grey background above. If there is a corresponding event in 1 process, we can define 1 function (of course, find the function prototype in MSDN first), and then in the
InitializeComponent adds to the list of events, as follows:


private void InitializeComponent()
{   
  this.Unload += new System.EventHandler(this.MainWebForm_Unload);
  this.Load += new System.EventHandler(this.Page_Load);
  this.Init += new System.EventHandler(this.Page_Init);
  this.PreRender += new System.EventHandler(this.My_PreRender);
}

For several procedures without corresponding events, such as 2.ES160en ViewState, we can override the virtual function of protected override void LoadViewState(object savedState). To add your own control code, but do not use base class corresponding methods, such as:

protected override void LoadViewState(object savedState)
{
  // Take care of VIEWSTATE
  base.LoadViewState (savedState);
}


Related articles: