Discussion on the use of static and viewstate in Asp.net

Source: Internet
Author: User
Discussion on the use of static and viewstate in Asp.net
 
Abstract: Asp.net is used to develop an application system based on the B/S mode, the data sharing problem is often solved when values are transmitted between function members of the same page class, between pages of the same session, and between pages of users of different machines. This can be achieved by using methods such as application, session, Cookie, static, and viewstate. Among them, the use of static variables is prone to problems. Poor use may lead to data disorder and cause potential faults to the system. Using viewstate as the page data transfer value is also a good choice. This issue is summarized and discussed based on the development and design practices.
Key words: Asp.net; programming; application research; object.
Graph classification: tp311.1 Document Identification Code: article A No.: 1008-7508 (2010) 09-0077-02
Use Asp.net to develop B/S-based application systems, the data sharing problem is often solved when values are transmitted between function members of the same page class, between pages of the same session, and between pages of users of different machines. This can be achieved by using methods such as application, session, Cookie, static, and viewstate. Among them, Stati C variables are prone to problems. Poor use may lead to data disorder and cause potential faults to the system. Using viewstate as the page data transfer value is also a good choice. This issue is summarized and discussed based on the development and design practices.
In C #, "When a member is defined with the static modifier, the static member is obtained. The static member belongs to the class and is shared by all instances of the class" ①. That is, all instances of this class share a static variable. The Asp.net page is a class. Every time a page is accessed, an instance of this class is instantiated on the server to respond to user requests. All instances share a static variable, which means that the static variable on the Asp.net page accessed by all clients is the same variable.
"The so-called viewstate is actually some key-value pairs. ASP. NET maintains the state of the webpage and server through it, and encapsulates viewstate into one or several hidden form fields and passes them to the client. When the client submits the request, the viewstate will also be submitted to the server. In this way, subsequent requests can obtain the status of the last request ." ②
Every time we refresh the Asp.net page, it is a brand new object, not the last object to be accessed. Therefore, the changes to the variables on the page were not retained during the last page access. When this problem occurs, the intuition sets this variable to static, so that the status of the variables on the page can be retained. However, this status is for a client (like the effect of session ). The result is that as long as a client changes this value, all other clients are affected (like applicatin ).
The reason should be discussed from the operating mechanism of Asp.net. In the C/S mode software development process, we usually do not care where the application runs, where the variables are stored, and the client program runs on the client, the server end runs on the server end. Generally, there is basically no sharing problem between the two except the data in the database. Therefore, the client users can safely use static variables because they are stored in the client program.
So we are used to using static variables for pages in B/S mode. We do not know that static in Asp.net is different from static in C/S. This is because all users in Asp.net will use the same static variable. This means that each user who uses the page performs operations on the variable will affect other users.
One solution is to select the viewstate object provided by Asp.net. Viewstate objects can be used to save various variables on the page, or even objects. "Some data can be directly stored in viewstate, such as strings, integers, Boolean values, tables in arrays, and hash tables ." ③ As long as the variable name is used for indexing, such as viewstate ["Var"], the value of the variable VAR can be accessed, regardless of whether VaR is a common variable or an object, it is even a datatabl e in the memory. The server creates a viewstate for each user connected to the page, so the viewstate is equivalent to a page-level session. It is equivalent to the global variable of the page, but it will be lost once the current page is exited.
Viewstate is easy to use, as shown below:
1. Save the variable to viewstate:
Viewstate ["times"] = times; // stores the common variable times.
Viewstate ["orders"] = dtorders; // stores dtorders, A able object.
2. Read the value in viewstate:
Times = (INT) viewstate ["times"];
Dtorders = (datatable) viewstate ["orders"];
Forced type conversion is required when reading the value of a variable, because when the variable (whether it is an int-type common variable times or a datatable object dtorders) all objects are stored in viewstate. So when we extract the viewstate, We must convert it to the corresponding type, otherwise an error will be reported. When the variable is saved to views Tate, the system automatically converts the variable.
This does not mean that static variables are useless. Classes declared using static in C # do not need to be instantiated for direct use. Because all users share the same static variable on the server, they can use static objects to access some common processing modules, such as type conversion and variable verification. So it depends on the specific situation.
Note: If you want to share one object or variable in multiple processes on the page, we cannot define a page-level global variable at the beginning of the page class, static can be used, but the variables of this type are not safe, so viewstate can be used.
Viewstate stores data in the page hidden control and no longer occupies server resources. Therefore, we can save some variables and objects that need to be remembered by the server to viewstate. Viewstate cannot store all. NET data. It only supports string, integer, Boolean, array, arraylist, hashtable, and custom data types.
Viewstate is usually used to save the status information of a single user, and the lifetime is equal to the lifetime of the page. Viewstate is used to pass values between functions on this page. Why is this method used because the page may be refreshed after an event occurs, if global variables are defined, viewstate must be used to keep data. Everything has two sides, because the viewstate variable in the client is actually using <input type = "hidden" value = "ADFAIB3P234P-AFAFAF ......" /> Save an object. In this way, if you want to save an object, even a very complex object (such as datatable), it will increase the burden of network transmission. Using viewstat e will increase the page HTML output and occupy more bandwidth, which requires careful consideration. In addition, because all the V iewstates are stored in a hidden domain, you can easily view the base6 4 encoded value by viewing the source code, after conversion, you can get the objects and variable values in your storage.
Viewstate can only upload values on one page (the session can pass values across multiple pages). viewstate is only valid in the front page, closing the current page, and then opening it again, the value saved by viewstate disappears. You need to keep the value of a variable when you access a page, and change its value at any time. It is better to use viewstate. Is viewstate used to synchronize the variable States between the client and the server? When two users operate on the same page, if static? When a serious data error occurs, it is normal to change to viewstate.
The system for developing the C/S mode is very different from the system for developing the B/S mode. Therefore, we must correctly understand the scope and survival of various variables based on different situations, so that we can correctly use various data storage methods without errors.

Note:
① ② Chen zhibo, ASP. NET database application development [M]. People's post and telecommunications Publishing House. 2005.
③ Xu Xinhua, proficient in ASP. net2.0 [M]. Mechanical Industry Press. 2006.
References:
[1] Liu Peiyi, ASP. NET Programming Tutorial [M]. Tsinghua University Press. 2009.
[2] Dong yige, ASP. NET Programming basics and Applications [M]. New address of kehai press. 2006.

Receipt date: 2010-09-06
Author profile: Yang Min (1963 ~), Female: Jilin City, associate professor of Changchun Automobile Industry College, mainly engaged in the teaching and research of automatic control technology. Liu danjie (1964 ~), Female: from Changchun, Jilin Province, associate professor of Changchun Automobile Industry College. He is mainly engaged in the teaching and research of automation control technology.

Related Article

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.