开发者

ASP.NET How to mantain variables value

开发者 https://www.devze.com 2023-01-04 18:04 出处:网络
Hy folks, i\'ve a big doubt... Since nwo, i\'ve always declared my aspx variable page like this Partial Public Class myPage

Hy folks, i've a big doubt... Since nwo, i've always declared my aspx variable page like this

Partial Public Class myPage
    Inherits System.Web.UI.Page

    Protected Shared Variable1 As Integer
    Protected Shared array1(10) As String

Declaring Protected Shared, my variable "mantain" their value for each function i call in my page, and they mantain their value in each postback.

But yesterday someone told me that that variable are SHARED for all users acce开发者_StackOverflow中文版ss my website.. (so user A see variables value of user B , user C etc.)

Can anyone help me to clarify ?


Your approach is almost certainly a bad idea.

A shared variable is instantiated once for your application, and retains the last value that was put in it, for any thread that accesses it. Carefully consider any code that is relying on the value of the variable - modifications to the value could be happening at any time, between any two lines of code, from any thread that is executing code that might modify it.

Shared doesn't have anything to do with visibility to end users per se. Shared means that there is only one instance of that variable, so every request that instantiates a myPage class will use the exact same memory for their Variable1 and array1 references. The Protected modifier ensures that no classes other than myPage or derivative classes can see and modify the variables, but any users executing a request that uses a myPage will certainly be using the same variable reference - and hence changing the value for any other users.

If you want to maintain values of a variable for a user's session, use the Session object, that's exactly what it's for.

Session["Variable1"] = "usersProtectedString";

If you want to maintain values for all users across the entire application, use the Application object.

Application.Lock();
Application["Variable1"] = "stringForAllUsers";
Application.Unlock();


Shared means shared. It can be accessed by other users at the same time. Try

Partial Public Class myPage
    Inherits System.Web.UI.Page

    Protected Shared Variable1 As String
    Protected Shared array1(10) As String

and initiate it

Dim mypage As New myPage
mypage.Variable1 = "My Protected String That No One Else Can Gain Access To"

If you need to maintain a variable across multiple pages, use a Session Variable.

Session("Variable1") = "My string that I can use all over the site but is still only accessible by me"

You can also store different types of objects in variables

Dim theDate as DateTime = DirectCast(Session("theDate"), DateTime)


AFAIK, there are 2 ways to share variables if desired in ASP.Net:

  1. Use the Application object as there is only one for the whole application pool.
  2. Use static variables. This one bit me in the butt bigtime a few years ago, so I did learn this lesson the hard way.


There's no particular reason why would these variables keep their state across postbacks. Certainly, they are not shared among users. If they retain values, it's because someone else saves them (perhaps in page viewstate?)

0

精彩评论

暂无评论...
验证码 换一张
取 消