The following is the initial process of the ASP.net page:
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 primarily to initialize the control, each time the page loads to perform this initial process, including the first and subsequent postback (here next postback, in fact, you can simply understand the user click submit button and so on, the form <Form> submitted to the server, This is a postback), here you can access the control, but the control value is not the value of the control we expect, he is just the initial value of a control (the default), for example: a TextBox1, we filled in "haha", after clicking Submit submitted the page, In Page_Init (), we visit the TextBox1.Text not our "haha", but the beginning of the "" "empty string, if the TextBox1 provided in our design when the default value, which is access to the default value provided, why, It's going to take a look at the next process.
The corresponding event Page.Init
2. Load ViewState
This process is loaded with ViewState and postback data, such as our top TextBox1, and then the "haha", so, in Post_init () to the control assignment is meaningless, it will be rewritten in this process, of course, the first page loaded with the exception, Because there is no viewstate data.
No corresponding event
3.Load postback data;
It says, postback can be understood as the user submits the form data, so this is the processing of form data, of course, here to design the control, the general situation does not want us to deal with this process, we skip. (In the previous brief description of the ASP.net page lifecycle, the process was put together with the load viewstate, which is the lifecycle process provided by Microsoft, which is presented separately to make everyone understand that this is a separate process)
No corresponding event
4. Page_Load ();
This process is also sure to execute every time the page is loaded, but the difference between attention and Page_Init is already covered, and here it is noted that page.ispostback is commonly used, indicating whether the page is being loaded in response to a client postback, or whether it is being loaded and accessed for the first time.
Copy Code code as follows:
private void Page_Load (object sender, System.EventArgs e)
{
if (! Page.IsPostBack)
{
Code here for the first time
}
Else
{
User submits form (ie postback) CODE here
}
Every time here, we're going to execute code here.
}
The corresponding event Page.load
5. Handle control events;
In this process, the corresponding specific control events, such as private void listBox1_SelectedIndexChanged (object sender, System.EventArgs e) events, etc.
There are no corresponding events (our own event functions are included in the process, such as the listbox1_selectedindexchanged above)
6. Page_
Pre-Presentation object, here is the penultimate step in presenting the data to the user program, and I estimate the meaning of the process, which is to modify the data to be presented to the user in the control properties and so on, which is also the final modification, the previous modification (e.g. in Page_Init) may be overwritten. This is done. Another operation is to save the state, that is, saveviewstate.
The corresponding event is Page.prerender
7. Page_render ();
We can see in the View->source, each page has a hidden <input>, this "__viewstate" is our server to write back page status information, before this, the server to render the page ( That is, to construct the HTML file, which is the data from the "__viewstate", and of course you notice that there is a Page.render event where we can add our own processing code, which means we can change the data, However, here is recommended not to modify here, since the provision of prerender, it should be in the final modification, of course, this is not necessary, but recommended!
The corresponding event Page.render
8. Unload event;
You should understand that when you want the server to request an object, it will generate an inherited page object in memory, which is the class of the page, which inherits from System.Web.UI.Page.
Occurs when a Page object is unloaded from memory, triggering the event.
The corresponding event Page.unload
9. Dispose method called;
Destroy all objects. Occurs when the page is freed from memory, which is the last stage of the lifetime. Maybe the 8th and 9 seem a little blurry, but I haven't figured out how to do it.
The corresponding event Dispose
The above is the description of the ASP.net page cycle.
Note the gray background text above, and if there is a corresponding event in a process, we can define a function (of course, we first find the function prototype in MSDN) and then
Add InitializeComponent to the list of events, as follows:
Copy Code code 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 processes that do not correspond to events, such as 2. Load ViewState, we can overload the page's virtual function protected override void LoadViewState (object savedstate), to add your own control code, but do not drop the corresponding method of the base class, such as:
Copy Code code as follows:
protected override void LoadViewState (object savedstate)
{
Handle ViewState Yourself
Base. LoadViewState (savedstate);
}