Asp.net static variable and viewstate use of with caution

  • 2020-05-24 05:21:29
  • OfStack

When developing CS software under the.Net platform, we often encounter the need to use the last modified values of certain variables later on. For the sake of simplicity, many people are used to using static to define these variables, and so am I. This is very convenient, the next time a function is called the variable still holds the processed value, just use it.

Now that we have moved on to BS software development, we naturally follow this habit. If you count the number of times a button has been pressed on the page, define the 1static variable times before the processing of the OnClick event in the class, then make times increase by 1 every time the OnClick event of the button is called, which is very convenient:
[C#]:

 
... 
static int times=0; 
... 
private void Button1_Click(object sender,EventArgs e) 
{ 
times++; 
Label1.Text=times.ToString(); 
} 

We don't realize we've planted an imperceptible time bomb when we're so glad it's so convenient. Why where?
This starts with the operating mechanism of Asp.net. In the software development process of CS mode, we usually don't care where the application is running, where the variables are stored, the client program is running on the client side, and the server program is running on the server side. In general, the two have nothing to share except the data in the database. So at this point the client user can safely use the static variable because it is stored in the client application.

Therefore, we are used to using static when doing BS mode pages, but we do not realize that static in Asp. static in net is different from static in CS. The reason is simple, because all users in Asp.net will use the same static variable. This means that for every user who USES the page, the operation on the variable will affect the other users. Take the counter example, suppose that times try a value of 0, because only our own in the use of this page at this time, will not have what problem, of course, but if two people are also connected to this page, if A click Button11 times, then B Label1 after refresh the page will display 1, click again if B Button11 times, times into 2, refresh the page after two problems: Both A and B will say, I only clicked Button11 times, how come Label1 shows me that I clicked Button1 twice? This is because two people share the same times on the server, and any action that one person does on times will be displayed in the browser of someone else using the page. That's the problem.

What to do? Fortunately, in addition to the Session object in the traditional Asp, Asp.net provides a better ViewState object. The ViewState object is used to hold various variables, even objects, on a page. The method of use is similar to HashTable, as long as the variable name is used as an index, such as ViewState["Var"], you can access the value of the variable Var, regardless of whether Var is a normal variable, object, or even a sheet of DataTable in memory, too convenient. Why can we use ViewState instead of static? The reason is that the server will set up one ViewState for each user connected to the page, so ViewState is equivalent to Session at the page level. We can safely use ViewState to access variables and objects that need to be temporarily stored.
The usage of ViewState is simple, as follows:
1. Save variables into ViewState:
"times" ViewState [] = times; // store the normal variable times
"Orders" ViewState [] = dtOrders; // store DataTable type objects dtOrders
2. Read the value in ViewState:
times=(int)ViewState["times"];
dtOrders=(DataTable)ViewState["Orders"];
See? It's as simple as that! Some friends ask why do you cast a variable when you read its value? This is because when a variable (int, times, DataTable, dtOrders) is stored in ViewState, ViewState treats it as Object, regardless of whether you are an ordinary variable or an object. So when we take something out of ViewState, 1 has to be converted to the appropriate type, otherwise it will report an error. Instead of saving variables with ViewState, the system will convert them automatically. (note that the string in ViewState brackets is only used to identify the index of a different variable, not necessarily the same name.) so the code for the counter above should look like this:
 
... 
ViewState["times"]=0; 
... 
private void Button1_Click(object sender,EventArgs e) 
{ 
int times=(int)ViewState["times"]; 
times++; 
ViewState["times"]=times; 
Label1.Text=times.ToString(); 
} 

1 as it goes down, it is more convenient to implement the object (or variable) to be saved to ViewState as a property. For the above counter times, this can be done:
 
... 
private int times 
{ 
get 
{ 
return (int)ViewState["times"]; 
} 
set 
{ 
ViewState["times"]=value; 
} 
} 
... 
private void Button1_Click(object sender,EventArgs e) 
{ 
times++; 
Label1.Text=times.ToString(); 
} 
... 

Here times will operate as a private property, isn't that convenient?
Does that mean that static type variables are useless? Of course not! Classes declared with static in C# are used without instantiation. Since all users share the same static variable on the server side, static objects can be used to access some common processing modules, such as type conversion, variable validation, and so on. So it depends on the situation.

One more thing to note: if multiple processes share an object or variable on a page, it is not acceptable to define a page-level global variable at the beginning of the page class.

Ok, now we can safely store some variables or objects.


Related articles: