ASP.NET - 受保护的变量
如果我使用受保护的变量,该变量是否存在于整个 Web 应用程序中,或者当用户通过 get 或 post 移动到其他页面时它是否会被删除?我确实知道它无法在其他页面中访问,除非我使用静态变量,但我很好奇它是否存在于整个应用程序中。请告诉我!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您移动到其他页面并返回时,将创建页面类的新实例,因此所有非静态变量将被重置。
该值将在一个请求进程生命周期内有效(从请求开始到请求结束),
使变量受到保护,仅意味着该变量在继承类中是可访问的。例如,在 ASP.NET 中,您可以在继承类中使用它,就像在标记中一样(因为它继承了类后面的代码),
这就是受保护变量的含义
,如果您想在页面之间保存一个值,您可以使用其中一项,具体取决于根据您的要求:
和 ViewState 在同一页面或控件的回发之间保留状态变量,而不会重定向到另一个页面。
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 :
and ViewState keeps state variable between postback in a same page or control while it is not redirected to another page.
protected
关键字无法确定变量存在的时间,也无法确定该变量在下一次回发中是否可用。您可能正在寻找的是状态管理。
查看此网页,了解如何在帖子之间保持状态背面、不同页面等。
另请查看此页面以确定在哪种情况下使用哪种状态管理功能。
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.
一般来说,“页面”变量仅在请求期间有效。如果您的变量是静态的,则在应用程序域卸载之前,所有请求都将只有一个变量实例。
如果您的变量是私有的或受保护的,则其他类将无法访问它。
不过,你的问题似乎有点奇怪。你担心什么?
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?