ASP. NET is different from winform such as Delphi, becauseProgramIs for one user, while ASP. NET is for multiple users.
Therefore, when you write a variable before a method or function in a program class, although it is a full-page variable, the value of this new value will be lost as long as the page is refreshed.
For example:
View code
Public class along: system. web. UI. page
{< br> string S = " A " ;
protected void button#click ()
{< br> S + = " B " ;< br> textbox1.text = s;
}< br> protected void button2_click ()
{< br> S + = " C " ;
textbox1.text = s;
}< BR >}
Public class along: system. web. UI. page <br/>{< br/> string S = "A"; <br/> protected void button#click () <br/>{< br/> S + = "B"; <br/> textbox1.text = s; <br/>}< br/> protected void button2_click () <br/>{< br/> S + = "C"; <br/> textbox1.text = s; <br/>}</P> <p>}You will find that every time you click the button, the value of S is not accumulated repeatedly, but only starts from the initialized.
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-side program runs on the server. Generally, the two programs have no sharing issues 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 ." You only need to use the variable name for indexing, such as viewstate ["Var"], to access the value of the variable VAR, regardless of whether VaR is a common variable, an object, or even a able 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; // store 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, however, 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 a serious error occurs in the use of static data, it will be normal after the change to viewstate.
This article Reprinted from: http://blog.csdn.net/fengyun14/article/details/6733994