我怎么能有一个全局变量(昏暗的AllTeams作为新的TeamList)只运行不回发 [英] how can I have a Global variable (dim AllTeams as New TeamList) only run not on postback

查看:63
本文介绍了我怎么能有一个全局变量(昏暗的AllTeams作为新的TeamList)只运行不回发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我有一个页面,在vb.net中我需要一个全局变量(AllTeams),因为我需要在整个codebehind.vb页面函数中使用这个变量。



有一个功能允许用户(所有值都存储在类中,没有数据库)来更新类的值,但是当页面被重新加载时,全局变量再次运行,它基本上就像没有发生更新一样开始。我试图在全局变量上添加if-then语句,但显然我不能在模块/函数之外这样做



我该怎么做?任何帮助将非常感激。



谢谢!

解决方案

.NET中没有全局变量这样的东西。 全局变量没有什么好处,所以只有没有这个垃圾就可以发布.NET。此外,您应该了解ASP.NET应用程序生命周期(就页面背后的代码而言)都位于HTTP请求和它生成的HTTP响应之间。 HTTP协议是无状态的:

http://en.wikipedia.org/wiki/HTTP [ ^ ],

http://en.wikipedia.org/wiki/Stateless_protocol [ ^ ]。



也就是说,要在页面之间进行通信,你需要无论如何要存储国家的东西。例如,它可以是存储在数据库中的状态。基于服务器的方法是会话状态管理。请从这里开始:

http://msdn.microsoft.com/en- us / library / ms178581.aspx [ ^ ]。



-SA


为了扩展谢尔盖所说的话,有几种方法可以在页面加载之间临时存储数据。不幸的是,所有这些都有它们的缺点。



可能最简单的方法是将数据保存在 Session 对象中。可以存储任何对象。应使用 Serializable 属性标记类和其他复杂对象。缺点是 Session 有重置的倾向,特别是如果你要存储大量的数据:在下一个页面加载时从一个页面保存数据是好的,但它是不够可靠被认为是全球性的。



下一个选项是cookie。在我看来,这确实不是一个非常好的选择:给定cookie的大小限制为4k,这可能不足以保存您的数据,并且浏览器可以设置为忽略cookie,这使得它非常不可靠。



您可以将数据压缩为ASCII格式并将其作为URL的一部分传递。这是在一些网络系统上完成的,以支持无cookie浏览器,但它有很多限制以及可能致命的安全漏洞(你可能不希望有人用加密数据为URL添加书签。)完全避免这种情况。



我经常使用的第四个选项是使用 Profile 结构。有一点学习曲线,但这实际上将您的Web应用程序与SQL数据库链接,其中基础结构由Framework管理。一旦到位,它很容易使用。它也更灵活,因为可以存储任何类型或数量的数据,并且保证数据一直持续到用户超时。



您也可以尝试使用自己的 Profile 替代方案,手动创建数据存储(XML结构,数据库条目,逗号分隔的文本文件,等等,以保存您的信息。问题是确保每个Web用户都具有随时间保持一致的ID - 例如,会话ID可以更改 - 并且定期清理旧文件。这些都是障碍,但可以克服。



在HTML的限制范围内,这些是选项。


您可以使用应用程序会话在用户之间共享信息。

Ok, so I have a page that in vb.net I needed a global variable (AllTeams) as I need this variable to be used throughout the entire codebehind.vb page functions.

There is a functionality that allows a user (all values are stored in a class, no database) to update the values of a class, but when the page is reloaded, the global variable runs again and it basically starts as if no update had occurred. I tried to put an if-then statement on the global variable, but apparently I cannot do so outside of modules/functions

How can I do this? Any help will be greatly appreciate.

Thank you!

解决方案

There are no such thing as "global variable" in .NET. There is nothing good about "global variables", so it's only good that .NET came out without this trash. Besides, you should understand that the ASP.NET application lifetime (in terms of the code behind of a page) all lies between HTTP request and HTTP response it generates. And HTTP protocol is stateless:
http://en.wikipedia.org/wiki/HTTP[^],
http://en.wikipedia.org/wiki/Stateless_protocol[^].

That said, to communicate between pages, you need something to store the state anyway. For example, it could be the state stored in a database. The server-based approach is the session state management. Please start here:
http://msdn.microsoft.com/en-us/library/ms178581.aspx[^].

—SA


To expand a bit on what Sergey said, there are several ways you can temporarily store data between page loads. Unfortunately, all of them have their drawbacks.

Probably the easiest is to save your data in the Session object. Any object can be stored. Classes and other complex objects should be tagged with the Serializable attribute. The downside is that Session has a tendency to reset, especially if you are storing large quantities: it is fine for holding data from one page while the next one loads, but it is not reliable enough to be considered "global."

The next option is cookies. This really isn't a very good option, in my opinion: the size of a given cookie is limited to 4k, which may not be big enough to hold your data, and browsers can be set to ignore cookies, which makes this very undependable.

You could compress the data into an ASCII format and pass it as part of the URL. This is done on some web systems to support cookieless browsers, but it has a lot of limitations as well as potentially fatal security flaws (you probably do not want someone bookmarking a URL with encrypted data.) Avoid this entirely.

The fourth option, which is what I will often use, is to use the Profile structure. There is a bit of a learning curve, but this essentially links your web app with a SQL database, with infrastructure managed by the Framework. Once it is in place, it is easy to use. It is also much more flexible, as any type or quantity of data can be stored and the data is guaranteed to persist until the user times out.

You could also try to roll your own Profile alternative, with you manually creating data stores (XML structures, database entries, comma delimited text files, whatever) as needed to hold your information. The problem is making sure that each web user has an ID that will remain consistent over time -- Session ID can change, for example -- and that old files get cleaned up regularly. These are hurdles, but can be overcome.

Within the limits of HTML, those are the options.


You can use application session to share the information between users.


这篇关于我怎么能有一个全局变量(昏暗的AllTeams作为新的TeamList)只运行不回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆