Exercise caution when using static variables in Asp.net

Source: Internet
Author: User
I am used to C/S programming and often define global variables. For convenience, I can still get the value of a variable the next time I call a function, we often define global variables of the static type. However, the bad habits of B/S programming are naturally used. When logging on to each page, all the global variables of the user's basic information are defined as static. As a result, a serious problem was found. That is, the user zhangsan logs on, displays the basic information of zhangsan, and adds a record to the user. As shown in the preceding example, the records added by Michael Jacob are displayed in the list, and the information displayed in the result is Li Si. This is the case of Shenma. The reason is that the static variable is a ghost, because after login by user Zhang San, Li Si will log on immediately. At this time, the value of the static variable on the server will be assigned a new value, instead of the name of Zhang San, don't replace Li Si. In this way, after Michael Jacob adds information, the database displays the data for Michael Jacob's list. The information of Li Si is naturally displayed. This seemingly strange problem actually implies the operational mechanism of Asp.net. In Asp.net, all users use the same static variable value, which is often used to record the number of visitors to the site. Each time one more person accesses the static variable, the value of the static variable is added. The historical person numbers displayed by all users are also changing. However, it cannot be used to record global variables for user login. Otherwise, the above problems will occur. A small global variable implies a potential data leakage killer. So how can I implement the page-level global variables of B/S for client users? Fortunately, in addition to session objects in traditional ASP, Asp.net provides a better viewstate object. Viewstate object is used to save various variables on the page, or even objects.Why can viewstate be used instead of static variables? The reason is that the server creates a viewstate for each user connected to the page, so viewstate is equivalent to a page-level session. Now we can safely use viewstate to access the variables and objects to be saved.HoweverTo use viewstate, you must have a server form tag (<form runat = Server>) on the ASPX page ). The younger brother was a senior this year. Not long ago, he developed a small system for the school. After the release, he found that the log on data of different users was messy. Later, I found the information on the Internet and found it was caused by the problem of global variables. Well, this system does not use confidential data, or it will be a tragedy. Now, we are eager to modify the static variable. In addition to the viewstate object method on the Internet. Have you ever encountered this problem before and implemented it in any better way? Or which of the following experts can explain in depth how the global variables (Session, cookies, application, cache, etc.) of Asp.net run. Thanks again and thanks again. Reference: http://weball.blog.sohu.com/103318100.html
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.