0

I have a static int that keeps an record of how many user control has been added to page.

    static int mycount = 1; //Default value

When I add more controls to the page the counter gets +1, that's working fine

    static int mycount = 2; //Add second control

But if I open another instance of the page in different browser the counter still has mycount=2

The static int sould have 1 as default if you open a new browser session.

Do I need to work with sessions or what?

//UPDATE

I used Sessions after I found an answer here on SO

How to access session variables from any class in ASP.NET?

Very good explanation on the session handling

Community
  • 1
  • 1
Jon Orn
  • 15
  • 5

2 Answers2

0

Yes, Definitely! memory for a static variable is allocated inside of the heap allocated for object-type. As far as we instantiate only one object-type for each page in memory makes it behave like this. For asp web page session state is more useful then static variable thus. The next post states the same with more details.

amdmax
  • 771
  • 3
  • 14
0

Absolutely I would recommend using something that not static because static is saved in the memory and may stay there for a while, in other way session variable are stored in the session that is usually just depends on the specific user therefore all session variable are unique for the user while static variable are more difficult to make unique.

Session["mycount"] = 1;

http://msdn.microsoft.com/en-us/library/ms178581.aspx Or based on your case it even better to use viewstate http://msdn.microsoft.com/en-us/library/4yfdwycw%28v=vs.71%29.aspx

ViewState["mycount"] = "1";
COLD TOLD
  • 13,513
  • 3
  • 35
  • 52