将元素传递到 c# 类
我之前问过一个类似的问题,所以如果这有点重复,我很抱歉。
这是我的问题,我有一个母版页,在页面加载时有一个标签,我将此控件传递给我拥有的全局类,类内部类似于:
global class:
private static label myLabel;
public label updateLabel
{
set { myLabel = value;}
get { return myLabel;}
}
在母版页加载事件上,我
global.updateLabel = labelOnMasterPage;
现在说我有一个控制应用程序中的其他地方,我说
global.updateLabel.Text = "my new text for label";
这将更新主页上的标签并且一切正常。现在我的这个只需要一个用户登录,并且这个应用程序将有多个用户,我的问题是,因为我在全局类静态中声明标签,如果我更新一个用户的标签,这会影响其他用户看到的内容?
有更好的方法吗?感谢您抽出时间。
I asked a sort of similar question to this before so I'm sorry if this is a bit repetitive.
Here is my question, I have a master page that has a label on page load I pass this control to a global class that I have, inside the class is something like:
global class:
private static label myLabel;
public label updateLabel
{
set { myLabel = value;}
get { return myLabel;}
}
on the master page load event i do
global.updateLabel = labelOnMasterPage;
now say that I have a control somewhere else in the application and I say
global.updateLabel.Text = "my new text for label";
this will update the label on the master page and everything works. Now my this is working with only one user logging in and this app will have multi users, my question is since I'm declaring the label in the global class static if I update the label on one user will this affect what the other users see?
Is there a better way to to this? Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这里的
static
将适用于访问它的所有代码,无论线程或用户如何。在像 ASP.NET 这样的高度线程环境中,您很少需要静态字段。您还人为地将标签实例的生命周期延长了长,超出了预期。
还有其他选择,例如用户会话或应用程序级别 - 但存储字符串(如果有的话),而不是标签对象。
Yes,
static
here will apply to all code that access it, regardless of thread or user. It is very rare that you want astatic
field in a highly threaded environment like ASP.NET.You are also artificially extending the lifetime of the label instance long beyond what was intended.
There are alternatives like the user's session or the application level - but store the string (if anything), not the label object.
如果它在静态类中,所有用户都会看到相同的内容。
由于线程数量的原因,在 ASP.net 应用程序中使用这种方法时需要小心。如果您真的想这样做,您可能应该考虑使用锁使其线程安全。
就我个人而言,我会将其存储在数据库中,这种方法看起来很hacky。
If it is in a static class, all users will see the same.
You need to be careful about using this kind of approach in an ASP.net application due to the number of threads. If you really want to do this, you should probably look to make it thread safe with a lock.
Personally I would store it in a database, this kind of approach seems very hacky.