A. What is the ASP . NET page life cycle
When we enter a URL in the browser's address bar, enter to view the page, a request is sent to the server side (IIS), the server will determine the requested page sent over, fully recognize the HTTP page handler class, the ASP. NET runtime will invoke the handler's The ProcessRequest method is used to process the request to create the Page object. Typically, you do not need to change the implementation of this method because it is provided by the Page class. Next, the ProcessRequest method of creating the Page object makes the page go through various stages: Initialize, load view state information and postback data, load user code for the page, and perform postback server-side events. After that, the page enters display mode: Collects the updated view state, generates the HTML code, and then sends the code to the output console. Finally, the page is uninstalled and the request is processed. The process of handling this series of events where the Page object ProcessRequest method completes is the ASP. NET page life cycle.
two. understand The benefits of the ASP. NET page life cycle
Knowing how to master the ASP. NET page life cycle is essential, which helps us to control the page more flexibly and develop it in the way we need it. Asp. NET page runs, this page goes through one chain of events after another, performing different behaviors in each chain of events, all of which together make up the pages we need. Knowing the life cycle also helps us to locate the problem where the program is being debugged.
three. an explanation of the ASP . NET page life cycle
A) Look first. The requested page went through those processes before starting a page life cycle. :
Communication processing Process:
When the server is running normally, when the client (browser) sends a request to the server side, the server is responsible for listening for the socket (socket), and after hearing a connection request, the client is established to communicate with the client for a socket for communication. When a user sends a request to the server in a browser, the browser translates the request into a request message that conforms to the HTTP protocol, in accordance with both agreed network Transport layer HTTP protocols. The device is then sent to the server according to the TCP protocol. After the request message is received on the server side. The message is parsed according to the HTTP protocol. If the requested content is static data, the server side directly will be the requested data, according to the HTTP protocol generated response message after the return to the browser. The browser then parses the received message. Displays the page to the browser window. (Note: Only one HTML frame is displayed at the beginning.) After many requests. Put CSS styles, pictures ..... A single load comes in to form a complete page. This process, there are multiple interactive communications. The socket used to communicate is destroyed when the communication ends, because the HTTP protocol is a stateless communication. This reduces the pressure on the server)
When the browser sends a request to the client as dynamic. Then the server (IIS) discovers that files that cannot handle this suffix will pass through the mapping table. The response implements the extensible program Asp.net_isapi.dll of the interface, then Asp.net_isapi.dll, and forwards the request to the website program for processing. Site Program Calls
The ProcessRequest method of the HttpRuntime class to handle:
1. This class parses the received request message. Encapsulates the request message into the properties of the class named HttpWorkerRequest (for other purposes).
2. Creates a HttpContext object. This object is the context of the currently requested message. It contains all the requested data. There are two objects of the most important class: HTTP. Request and Http.response. There are also two properties in the HttpRequest object. form (data parameters requested by the form). Querystring (data parameters passed by URL); The value of the parameter can be found through the indexer. There is a TextWriter object in the HttpResponse object. The data that is stored in the requested page is output to the browser during execution. It can be output to the browser through the Write method.
3. Through a static method of the HttpApplicationFactory class. To create a separate HttpApplication object for each request. Before creation Httpapplicationfactor will be viewed in the HttpApplication pool, there is no idle. If there is a direct use. Create a new HttpApplication when you don't have it.
4. In HttpApplication, the ProcessRequest method of the requested page object is converted into an interface IHttpHandler by reflection.
The method executes as follows : Executes the ProcessRequest method in HttpApplication first. This method contains 19 delegate events to be executed sequentially when the requested page object is created when the 8th event is executed, between the 11th and 12th events. Executes the ProcessRequest method of the requested page object that was created.
II) Start Page life cycle
1. The main phases of the page life cycle include: Phase events/Methods
Page Initialization Page_Init
Load view state LoadViewState
Postback data processing LoadPostData
Page load Page_Load
Postback notification RaisePostDataChangedEvent
Postback Event Handling RaisePostBackEvent
Page pre-rendering Page_PreRender
Save ViewState SaveViewState
Page Rendering Page_render
Page Uninstall Page_Unload
2. Main events of the page life cycle:
PreInit:
1. Check the IsPostBack property
2. Dynamically set the master Page
3. Dynamic Settings Theme
4. Set default values for controls (UniqueID, etc.)
5. Recreate the dynamic control (initialize the control), initialize the value of the control
Init: This event occurs when all the controls are initialized and all skin settings are applied later. It is used to read or initialize the properties of a control. It can be used to register events for some of the controls that are not indicated in the ASPX page.
Initcomplete:use this event for processing tasks, require all initialization to is complete.
Preload: Loads the viewstate of the page and all the controls, and then processes all postback data contained in the request instance.
Load: This event is probably the most familiar of all. Note that the Page object recursively invokes the OnLoad event of the child control until the page and all child controls are loaded to completion. This event is primarily used to set the value of a control's properties and to establish a database connection (typically not).
Validation: If your control requires validation, validation will occur at this stage, and you can check the control's IsValid property at this point. The event associated with this phase is validate, which has an overloaded method that can accept the validation string group (overload method), which performs validation of a particular control group.
Control events: This is not much to say, mainly the event that handles the control, such as Click. This also lets us understand that every time we click a button, we actually have to execute the Load event before executing the Click event, generally we use! IsPostBack to avoid performing unnecessary load logic.
LoadComplete: All the controls on the page are loaded and executed later, and for the time being they don't think about what to do ...
PreRender: This is the last event before HTML is generated. The controls in each page have a prerender process. This is where you make the last modification to the HTML results that will be output.
Savestatecomplete: Before this time occurs, all controls and pages have been saved, and any changes to the page or control will not produce left or right. I have no idea what to do for a moment.
Render: It is not an event but a method. The job is to write the HTML back to the client browser.
UnLoad: This happens with every control on the page. In the control, use this event to do cleanup work, such as shutting down the database connection, and so on. The same is done with the page itself, such as closing open files and database connections, or ending logs or other specified work.
It is necessary to note that each request creates an instance of the new page class, so that the field you define in the page cannot pass the value in two request, it needs to be stored using ViewState.
Overall process diagram for page processing
ASP. NET page Life cycle Summary
1 First picture from the global description a request is sent from the client, and the HTTP. SYS component in the server Windows kernel receives the request to start until IIS finishes processing the request and responds to the end of the client.
2 The second picture is the detailed steps for the HTTP processing pipeline in Figure 1
3 The third picture is the detailed life-cycle process of the call handler (HttpHandler, here the Page object) in the processing pipeline in Figure 2Http.
Figure 1 below:
Figure 2 below:
Figure 3 below:
ASP. NET page life cycle