Static fields across .aspx pages

G

Guest

Hi All,

I'm a beginner with ASP.Net so please bear with me. I'm having difficulties
trying to understand the behaviour of static variable across multiple ASP.Net
page. My static variable is a Hashtable and located in a dll which is
referenced by my ASP.Net aspx pages.

In one pages I have a form and in the form submit I remove some items from
the Hashtable depending on user's data. On another button in this form I do a
Response.Redirect("another.aspx") to another aspx page.

Strangely enough on the second redirected aspx page my Hashtable shows a
different value from the one that I have modified. Why does this happen?
Shouldn't a static variable be static across all ASP.Net sessions?

Thanks,
 
G

Guest

Hi

If in your static hashtable all the user can modify the same object this
will be fatal for your app. So it can happen that you set a variable on a
session and when you make a postback someone to set the same variable and
you'll see his value.

If your hashkey identify a specific session then this will have no problem
because you'll simulate the session. I think it's a better option to create a
hashtable for every user, which contain user properties, an save this in
session. And just if you have a variable which is not modified by any user,
but it's access by all users, use static field.

hope helps
Cheers
 
H

Hans Kesting

Victor said:
Hi All,

I'm a beginner with ASP.Net so please bear with me. I'm having
difficulties trying to understand the behaviour of static variable
across multiple ASP.Net page. My static variable is a Hashtable and
located in a dll which is referenced by my ASP.Net aspx pages.

In one pages I have a form and in the form submit I remove some items
from the Hashtable depending on user's data. On another button in
this form I do a Response.Redirect("another.aspx") to another aspx
page.

Strangely enough on the second redirected aspx page my Hashtable
shows a different value from the one that I have modified. Why does
this happen? Shouldn't a static variable be static across all ASP.Net
sessions?

Thanks,

Please show some code.

How do you declare that static variable (or variableS?), how do you
access it? When and how do you initialize it?

Hans Kesting
 
V

Victor Hadianto

Hi,

Yep I know the issue, but this is really a simple system. I thought using a
simple static hashtable will be enough for this situation.
 
V

Victor Hadianto

Ok right it's quiet simple actually. Like I said the Hashtable is declared
on a different assembly. So the declaration is just:

public class SimpleData
{
public static Hashtable Data = new Hashtable();
}


And in edit.aspx on the btnSave submit event I modify the data like this:

SimpleData.Data["field1"] = ...
SimpleData.Data["field2"] = ...

In that edit.aspx I also have a redirect button that simply does the
following:

Response.Redirect("view.aspx")

Now in the view.aspx load event I check SimpleData.Data Hashtable and the
values are different from the ones that I've just put it.

That's it.
 
G

Guest

your example works fine on my machine.
try to put a new key in your hashtable which is not used in your app and see
if it happens the same with it.
 
V

Victor Hadianto

You are right actually, apology. My problem was I didn't take into account
that page load event is called again when I click on the redirection button.
That was the problem, I fixed the code and now it's fine thanks.

--
Victor Hadianto
http://www.synop.com/Products/SauceReader/

Psycho said:
your example works fine on my machine.
try to put a new key in your hashtable which is not used in your app and
see
if it happens the same with it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top