Asp.net Server Control Programming (3) viewstate (1) --asp.net Control

Source: Internet
Author: User

We all know that the Web application is stateless. After a client request is completed, the server deletes the request information. Sometimes we need to keep the information after the Web request is completed. In traditional WEB programming, the most common two methods are session and cookies, session will increase the burden on the server, and cookies will depend on the client, requiring the client to support cookies. At the same time, cookies are stored on the client computer, so there may be security issues.
In Asp.net, Ms proposes a new State persistence, Which is viewstate. Of course, the original session and cookies can be used in Asp.net, let's use a web request to illustrate a workflow of viewstate (only care about viewstate), even if we do not write the Asp.net Server Control, however, I 'd like to know whether the request and return of the page are good for the Asp.net program.

The client requests a new ASPX page from the server:
1. The server creates a control tree, creates all controls on the page, and adds the controls to the Control tree.
2. trackviewstate status tracking: Check the enableviewstate attribute of the control and track the controls with enableviewstate = ture.
3. Trigger page onload.
4. Pre-generate onprerender.
5. Check the enableviewstate attribute of the control and call saveviewstate [State 1] to save the state.
6. Render generation.
7 ,... ...

Return An ASPX page from the client. The page is post to the server.
A. Same as above 1.
B. Same as above 2.
C. loadviewstate. The status before the control is submitted is [Load Status 1].
D. loadpostdata: extract the corresponding data from the form returned by post, and update the control status [status 2].
E. Same as above 3.
F. Modify and return events are triggered.
G.
H. Save the saveviewstate [State 2].
I, render.
J ,... ...

The above is a process of viewstate work, which is actually the life cycle of Aspx. Although we store viewstate in 5 and H, we load viewstate in C, then, how is it stored and loaded? This is the serialization and parallelization of viewstate. viewstate is serialized at 5 and H to serialize these States into a string (such as value = "/callback When the page is post back to the server, the string is parallelized in C and deserialized into the object state. Aspx pages are serialized and parallelized. At the same time, the costs of these two processes are relatively high. My principle is to save the viewstate of the control as little as possible. What kind of object can be saved in viewstate? In principle, all objects that can be serialized can be stored in viewstate, but viewstate in Asp.net optimizes array, arraylist, and hashtable.
Viewstate is transmitted in plain text during the request and return process of the ASPX page, which is easily intercepted by others. For especially important information, encryption or session storage can be considered, however, this is not the responsibility of the control developer, but should be done by the page developer.
Finally: Why do we need to return An ASPX page with a form running runat = "server"? We can see from the above d that, we need this form to upload the updated data of the control back and forth. At the same time, we can also see that all viewstate processing is performed by the server and has no relationship with the client, whether you are an IE client or a Firefox client, you can access the published Asp.net site.
If you find anything inappropriate in this article, ask your friends for advice. Thank you first.

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.