(11) Several ways to save the state of the Web program, application,session,cookie,viewstate

Source: Internet
Author: User
Tags server memory

There are several ways that a Web program saves state:

1.Application: The data stored in application is globally valid, and application should be stored in the data that has less access and is used by at least a large number of functions, such as counters or database connection strings.

Application.Lock ();
((int) application["Count"]) + +;
Application.UnLock ();

2.Session: Within ASP., there is a stateapplication to manage the Session

3.Cookie

4.ViewState: Actually ViewState is not mysterious, it is a hidden field, but he is the base of server control state preservation

Here are some of the following:

ViewState

For example:

<formname= "Form1"Method= "POST"Action= "Modi_user_info.aspx"language= "JavaScript"onsubmit= "Javascript:return webform_onsubmit ();"ID= "Form1"><inputtype= "hidden"name= "__eventtarget"ID= "__eventtarget"value="" /><inputtype= "hidden"name= "__eventargument"ID= "__eventargument"value=""/><inputtype= "hidden"name= "__viewstate"ID= "__viewstate"value= "/wepdwukltizodm0nzq5oq9kfgicaq9kfgrmdw8wah4evgv4dauhamfzbwluzwrkaggpdxychwafdda1otewmdawmdyxm2rkza==" />

People who have done web programs may have this painful experience, sometimes in order to deal with the more complex features on the page,

Often add a lot of hidden, and then on the server side with a lot of judgment to analyze the current state, writing annoying,

Writing the code is ugly; in fact, ViewState is helping us implement the function of saving control state.

, the server-side control can save state across multiple requests and rely on it.

1, ViewState is stored in the client, it will reduce the burden of the server, is a better way to save data

Way.
2, because of the limitations of the viewstate itself, can only save the object serializable, and it is best not to put too much east

West, can save the province, so as not to slow down the speed of transmission, and increase the burden of server parsing.
3, we can be in a very simple way to get the value inside the viewstate, we discussed above a

Some, although the parsing code is not written out, but the use of losformatter can get viewstate inverse sequence

After the object, it is easy to parse out, so viewstate on the security or comparison, it is recommended not

Store more confidential and sensitive information, although viewstate can be encrypted, but because viewstate is stored in the guest

Customers, there is a natural danger of security.
[C #]
Save in ViewState
viewstate["SortOrder"] = "DESC"; String 5

Read from ViewState
String sortOrder = (string) viewstate["SortOrder"];

If you want to use ViewState, you must have a server-side form tag in the ASPX page (<form

runat=server>). A form field is required so that a hidden field containing ViewState information can be returned

to the server. Also, the form must be a server-side form, so that the page is executed on the server

, the ASP. NET page framework can add hidden fields.

In the case of the page does not return, you can omit the server-side <form> tag to remove the page

ViewState.

Session

The session value is stored on the server memory, so, to be sure, a large number of

(11) Several ways to save the state of the Web program, application,session,cookie,viewstate

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.