By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,667 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,667 IT Pros & Developers. It's quick & easy.

Static fields across .aspx pages

P: n/a
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,

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

Nov 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

"Victor Hadianto" wrote:
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,

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

Nov 18 '05 #2

P: n/a
Victor Hadianto wrote:
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
Nov 18 '05 #3

P: n/a
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.

--
Victor Hadianto
http://www.synop.com/Products/SauceReader/
"Psycho" <Ps****@discussions.microsoft.com> wrote in message
news:02**********************************@microsof t.com...
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

"Victor Hadianto" wrote:
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,

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

Nov 18 '05 #4

P: n/a
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.

--
Victor Hadianto
http://www.synop.com/Products/SauceReader/
"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
news:uD*************@tk2msftngp13.phx.gbl...
Victor Hadianto wrote:
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

Nov 18 '05 #5

P: n/a
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.

"Victor Hadianto" wrote:
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.

--
Victor Hadianto
http://www.synop.com/Products/SauceReader/
"Psycho" <Ps****@discussions.microsoft.com> wrote in message
news:02**********************************@microsof t.com...
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

"Victor Hadianto" wrote:
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,

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


Nov 18 '05 #6

P: n/a
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" <Ps****@discussions.microsoft.com> wrote in message
news:F8**********************************@microsof t.com...
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.

"Victor Hadianto" wrote:
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.

--
Victor Hadianto
http://www.synop.com/Products/SauceReader/
"Psycho" <Ps****@discussions.microsoft.com> wrote in message
news:02**********************************@microsof t.com...
> 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
>
>
>
> "Victor Hadianto" wrote:
>
>> 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,
>>
>> --
>> Victor Hadianto
>> http://www.synop.com/Products/SauceReader/
>>


Nov 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.