passing elements into class c#
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 updat开发者_JAVA百科e 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.
Yes, static
here will apply to all code that access it, regardless of thread or user. It is very rare that you want a static
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.
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.
精彩评论