在 ASP.NET 代码隐藏中声明全局变量
我想声明一个 Dictionary
变量,但不知道在哪里/如何声明。字典中的值将是页面中的对象(ListBoxes、DropDownLists 等),因此我无法在其他地方准确创建辅助类。有什么方法可以让代码隐藏中的每个方法访问此变量吗?
I want to declare a Dictionary<string, object>
variable but don't know where/how to. The values in the dictionary will be objects from the Page (ListBoxes, DropDownLists, etc) so I can't exactly create a helper class somewhere else. Is there any way I can make this variable accessible from each method in the codebehind?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以在三个位置存储数据。在应用程序级别,这使得所有会话都可以访问数据。会话级别,使数据可供该特定用户的所有页面使用。或者,页面级别,使其在回发之间可用于当前页面。请参阅下面的示例:
在应用程序级别存储
封装存储的示例类:
用法示例(在页面加载事件中):
这将增加所有会话的值。要尝试它,请在您的计算机上打开两个浏览器并使用相同的 URL。请注意该值如何针对每个用户的请求递增。
在会话级别存储:
封装存储的示例类:
用法示例(在页面加载事件中):
当前用户会话的值会增加。当服务器上运行另一个会话时,它不会增加。
页面级存储
页面内:
示例用法
页面加载处理程序中的
单击按钮时
:请记住,ViewState 在页面加载时不会反序列化,但它将在 Button.Click 等事件处理程序中反序列化
所有代码均已测试,如果您想要完整的解决方案,请告诉我,我会通过电子邮件发送给您。
There are three places where you can store data. At the application level, which makes the data accessible to all sessions. The session level, which makes the data available to all pages for that specific user. Or, the page level, which makes it available to the current page, between postbacks. See examples below:
Storing at Application Level
Sample Class to encapsulate storage:
Usage Sample (in Page Load event):
This will increment the value across all sessions. To try it, open two browsers on your machine and it the same URL. Notice how the value is incremented for each user's request.
Storing at Session Level:
Sample Class to encapsulate storage:
Usage Sample (in Page Load event):
Value is incremented for the current user's session. It will not increment when another session is running on the server.
Page Level Storage
Within the page:
Sample Usage
in Page Load handler
on button click:
Keep in mind, that the ViewState is not Deserialized on Page Load, however it will be deserialized in event handlers like Button.Click
All code has been tested, if you want the full solution, let me know, I will email it to you.
在类内部但在任何方法外部声明变量。例如:
Declare the variable inside the class, but outside of any method. for Example:
关于数据类型、要存储多长时间等有多种选项。请查看 会话状态,视图状态,应用程序状态
基于您的全局变量要求我能想到两种可能性。
使用静态类和静态变量,如下面的代码所示。
现在使用它
2 使用
Application
状态来存储您的全局数据。如下。There are multiple options on what kind of data, how long you'd want to store etc. look at Session State, ViewState, Application State
Based on your Global Variable requirement I can think of two possibilities.
Use a static class and a static variable as shown in below code.
Now using it
2 use
Application
state to store your global data. as below.您可以创建一个必须是公共的类文件(例如general.cs),以便可以轻松访问它。在该类文件中,您可以定义这个全局变量。
您可以从任何其他页面或类访问此变量,因为它是公共定义的,并且可以通过创建类的实例来使用此全局变量。
You can create one class file that must be public(say general.cs), so that it can be easily accessible. And in that class file you can define this global variable.
You can access this variable from any of the other pages or Class as it is defined public and can utilize this globally variable by creating the instance of the class.
您可以在类声明行之后的代码隐藏行中声明变量。如果您只想在一页上使用它,这将起作用。
You can declare the variable in the codebehind on the line right after the class declaration line. This will work if you just want to use that on one page.