ASP.NET - protected variable

爷,独闯天下 提交于 2020-01-04 05:33:07

问题


If I use a protected variable, does the variable exist for the whole web application or does it get removed when the user move to other page by get or post? I do know that it is not accessible in other pages unless I use static variable, but I am curious as to if it exists for the whole application. Please let me know!


回答1:


when you move to other page and return, a new instance of your page class will be created and so all non static variables will be reset.

The value will be valid in a one request process life time (starts with request start and ends with request end)

making a variable protected, just means that this variable is access-able in inherited class. for example in asp.net you can use it in inherited class like inside your markup (because it inherits code behind class)

this is the meaning of protected variable

if you want to keep a value saved between pages you can use one of these items depending on your requirement :

  • Cookie
  • Query String
  • Session States
  • Application States
  • Cache

and ViewState keeps state variable between postback in a same page or control while it is not redirected to another page.




回答2:


protected keyword does not determine how long a variable exists nor does it determine whether it will be available in the next post back.

What you are probably looking for is state management.

Take a look at this webpage to see how you can maintain state between post backs, different pages etc.

Also take a look at this page to determine which state management feature to use in which situation.




回答3:


In general, "page" variables only live through the duration of the request. If your variable is static, there will only be one instance of the variable for all the requests until the app domain unloads.

If your variable is private or protected, no other classes will have access to it.

Your question, however, seems a little strange. What's your concern?



来源:https://stackoverflow.com/questions/5609771/asp-net-protected-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!