Mastering the life cycle and state management of Web Forms

Source: Internet
Author: User
Tags resource client
Web If you have the experience of using Active Server Pages (ASP), you will realize that ASP is a linear processing model. ASP pages are processed in order from top to bottom. Each line of ASP code and static HTML is processed in the order in which it is displayed in the file. A user action in a round trip causes the page to be sent to the server. Because this operation causes a round trip, the server must re-create the page. After the page is re-created, the page is processed in the same order as before, so the page does not show true event-driven behavior. To create an event-driven experience, you need to explicitly design it. In addition, you must explicitly maintain page and control state at the most basic level. This model limits the richness of the user interface that can be combined, and this model adds complexity to the code that needs to support it.

In contrast, the event-driven model in traditional Visual Basic applications contains programmable elements that can be initialized and displayed on a form. Users interact with these elements, which in turn causes the event that invokes the event handler to be raised. This model supports true event-driven behavior, which greatly broadens the richness of the user interface that can be combined and reduces the complexity of the code needed to support it.

ASP.net replaces the linear processing model of ASP by simulating the behavior of event-driven model. Provides an ASP.net page framework to implicitly establish an association of event and event handlers for you. With this page framework, you can easily create a user interface that responds to user actions.

First, we need to understand the life cycle of Web forms to better understand the state management of Web Forms.

The life cycle of a Web form is similar to the life cycle of a Web process running on a server. A Web Form allocates memory space from instantiation to processing end frees up memory, typically through the following 4 steps: initialization, page loading, event handling, and resource cleanup.

(1) Page initialization

The first event Page_Init occurs when the page is initialized. The system performs all the initialization steps required to create and set up an example.

(2) Page load

Page mount after initialization, the event that occurs is called Page_Load, and it is used as follows:

Check whether the page is being processed for the first time according to the Page.IsPostBack property;

The data bundle is executed the first time the page is processed, or the data bundle expression is again judged in the later cycle;

Read and update control properties;

Restores the status of the previous client request saved in the Save step;

(3) Event handling

Each action on the Web form activates an event that arrives at the server. A Web Form has two views: a customer View and a server view. All data processing is done on the server. When an event is caused by clicking the mouse or another method, the event arrives at the server and returns the corresponding data.

(4) Resource cleanup

The last step occurs when a form completes its task and is ready to uninstall, activating the Page_Unload event to complete the final resource cleanup, such as closing the file, closing the database connection, and discarding the object.

We know that web-based access is disconnected, and Web pages are stateless. And in the ASP in the linear drive mode is different is asp.net using the event-driven mode, then, Web Forms How to do asp.net page state maintenance? In ASP.net, a concept is introduced: view state, which saves the status information of the page using view.

In addition to the first access request is a base class generation, the subsequent return of each client is based on the client page state to re-create the page. and the various state information of the page is saved in the hidden field of the client page source file <input type= "hidden" name= "__viewstate" value= ""/>, we can see it through the browser, Value is the encrypted page state information. The server reproduces the form based on the value values, and then executes the event procedure that performs the response in the form's event-handling phase, and then returns the execution result to the client and changes the status value of the Web form and writes back to the value value. Finally, the resource cleanup phase is reached. The whole process is the life cycle of a Web form, and it also completes the management of the page state.

The following example shows how the ASP.net page frame maintains the state of the page. All the code is as follows:

<%@ Page language= "VB"%>
<script runat= "Server" >
Sub Button1_Click (sender as Object, E as EventArgs)
' The value of ViewState (' value ') is increased by 1
ViewState ("value") + + 1
Label1.Text = "This is the first" & Str (ViewState ("value")) & "click button"
End Sub
Sub Page_Load (sender as Object, E as EventArgs)
' Determines whether the page is processed for the first time
' If so, the value of the ViewState class to hold the key is 0
If not Page.IsPostBack Then
ViewState ("value") = 0
Label1.Text = "This is the first" & Str (ViewState ("value")) & "click button"
End If
End Sub
</script>
<title> Page State Management </title>
<body ms_positioning= "GridLayout" >
<form id= "Form1" method= "POST" runat= "Server" >
<asp:label id= "Label1" runat= "Server" width= "176px" ></asp:Label>
<br/>
<asp:button id= "Button1" runat= "Server" text= "click Add" ></asp:Button>
</form>
</body>

Save As Viewstate.aspx, the results of the run are shown in Figure 4.9.

  


Figure 4.9 After clicking the 3-time button

In the client's browser, we view the source file, and the values of the hidden fields that can be viewed are:
<input type= "hidden" name= __viewstate "value=" Ddwtotk1mje0nda4o3q8cdxsphzhbhvloz47bdxppdm+oz4+o2w8atwxp
Js+o2w8ddw7bdxppde+oz47bdx0pha8cdxspfrlehq7pjtspoi/meayr+esrcaz54k5
5ye75oyj6zkuoz4+oz47oz47pj47pj47pmlc5ptevwjok9dsup0e5ruygumi "/>
and the entire page's status information is



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.