Lifecycle of ASP. NET pages

Source: Internet
Author: User

When the Asp.net page is running, it will go through a declaration cycle, during which a series of operations will be performed and a series of methods will be called. Understanding the lifecycle of an Asp.net page is important for precisely controlling the display mode and behavior of page controls. Generally, a regular page goes through the following lifecycle stages:

Phase Description
Page request Page requests occur before the page lifecycle starts. ASP. net will determine whether the page needs to be analyzed and compiled (to start the page's lifecycle), or whether the Cache version of the page can be sent in response without running the page
Start In the initial stage, page properties such as request and response are set. In this phase, the page also determines whether the request is a send-back request or a new request, and sets the ispostback attribute. In addition, during the start phase, the uiculture attribute of the page will be set
Page Initialization During page initialization, you can use controls on the page and set the uniqueid attribute for each control. In addition, any topic will be applied to the page. If the current request is a send-back request, the send-back data has not been loaded, and the control property value has not been returned as a value in the view State.
Load During the loading, if the current request is a send-back request, the control properties will be loaded using information restored from the view status and control status.
Verify All verifications will be called during verificationProgramThe validate method of the Control. This method sets the isvalid attribute of each validator control and page.
Event Processing If the request is a send-back request, all event handlers are called.
Rendering During rendering, the view status is saved to the page, and each control is called on the page to provide the output to the outputstream of the page's response attribute.
Uninstall When the page is fully rendered, the page is sent to the client, and you are about to discard the page, the call is uninstalled. In this case, the page properties (such as response and request) will be uninstalled and cleaned up.

 

In the lifecycle of a page, there are generally the following events:

 

Page events TYPICAL USE
Page_preinit Use 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 attribute is set in this phase, the value may be overwritten in the next phase.
Page_init Read or initialize Control Properties
Page_load Read and update control properties
Control events Execute application-specific processing:
If the page contains the validators control, check the page and the isvalid attribute of each verification control before performing any processing.
Process specific events, such as click events of the button control.
Page_prerender Make the final changes to the page content
Page_unload The final cleanup may include:
Close the connection between open files and databases. Complete logging or other request-specific tasks.

 

In addition, web applications are stateless. Each time you request a new page or refresh the Page Server, a new instance of the current page will be created, which means you cannot obtain the previous information of the page. If you do need to do this, additional mechanisms are required. For example, to create a single page of index, add the followingCode:

Get current time

<% @ Page Language = " C # "   %>  
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/
Xhtml1/DTD/xhtml1-transitional.dtd" >  
< Script Runat = "Server" >  
String date;
Protected Void Page_load (Object sender, eventargs E)
{
If (Date =   Null ) // If date is null, it is set to the string format of the current time.
{
Date = Datetime. Now. tostring ();
}
Response. Write ( " Current Time: " + Date );
}
</ Script >  
< Html Xmlns = "Http://www.w3.org/1999/xhtml"   >  
< Head Runat = "Server" >  
< Title > No title page </ Title >  
</ Head >  
< Body >  
< Form ID = "Form1" Runat = "Server" >  
< Div >  

</ Div >  
</ Form >  
</ Body >  
</ Html >

 

According to our normal understanding, when the date string is null during the first operation, it is set to the current string representation of the system and output. When refresh is performed again, the date string is no longer empty, will still output the time string, but the result is not like this. Result of the first running:

Result After page refreshing:

This proves that an instance of the current page will be regenerated even when the current page is refreshed, because the date string variable is null only when a new instance of the page is generated, will be reset.

 

Ispostback attributes

The page class has an ispostback attribute that indicates whether the current page is loaded for the first time or responds to a server event of a control on the page, resulting in sending and loading.

Differences between first loading and sending back

<% @ Page Language = " C # "   %>

<!Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

< Script Runat = "Server" >
String data;
Protected Void Page_load (Object sender, eventargs E)
{
If (Data =   Null )
{
Data = Datetime. Now. tostring ();
}
Response. Write ( " The current time is: " + Data );
If ( ! Page. ispostback)
{
Response. Write ( " This is the first loading " );
}
Else
{
Response. Write ( " Respond to client sending and Load " );
}
}

ProtectedVoidBtnsubmit_click (Object sender, eventargs E)
{

}
</Script>

< Html Xmlns = "Http://www.w3.org/1999/xhtml" >
< Head Runat = "Server" >
< Title > </ Title >
</ Head >
< Body >
< Form ID = "Form1" Runat = "Server" >
< Div >

< ASP: button ID = "Btnsubmit" Runat = "Server" Onclick = "Btnsubmit_click" Text = "Submit"   />

</ Div >
</ Form >
</ Body >
</ Html >

Result of the first running of the page:

Refresh the page as follows:

 

Click "Submit:

Refresh the page as follows:

Each time a page is opened and refreshed, the effect is the same. The ispostback attribute is true only when the response client is sent back. Understanding this attribute and the server adopts a mechanism to "record" the status of the server control. (In fact, The viewstate and controlState mechanisms are used.) This will play a significant role in future data binding. A page instance needs to be re-generated for Refresh, but the re-sending request is to load the control attributes using the information restored from the view status and control status. Refreshing is not the first loading, but pressing enter in the address bar will become the first loading.

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.