Retaining values after a postback

  • Thread starter Thread starter PokerMan
  • Start date Start date
P

PokerMan

Hi

I have 6 variables that i want to keep after a postback. I have achieved it
by making them static, is this correct procedure?

Thanks
 
Hi

I have 6 variables that i want to keep after a postback. I have achieved it
by making them static, is this correct procedure?

Thanks

Depends on where they are, and what you want their scope to be.

If you want them to stick around for the entire life time of the
user's session then you should put them in the Session object (it's a
member of the Page, and also available from other classes via
HttpContext.Current.Session).

If you're coding in C# (sounds like you are), don't do that. Static
(Shared) in VB, means that all users of your site will be modifying
the same copy of the variable. If two users are signed on, they'll
walk all over that variable, and you'll get very strange results.

Hope this helps,
Mike
 
Thanks Mike, and yes your right static will do that ....obviously! oops lol

Its on a product details page, and i want to retain values such as product
id, only for that page. So there is something in Page i can use? Could you
give me an example of how you would in such a situation. No need for full
code, just a line would do (assuming its that simple).
 
Its on a product details page, and i want to retain values such as product
id, only for that page. So there is something in Page i can use? Could you
give me an example of how you would in such a situation. No need for full
code, just a line would do (assuming its that simple).

That's precisely what ViewState is for - persisting variables across
postback.
 
Thanks Mark.....just found it on google and yes exactly what i am after. For
anyone else stuck as i was:

ViewState.Add <-- to add a string key and a value pair

then ViewState["keyname"] to retrieve it after postback.

Thanks all!
 
Thanks Mike, and yes your right static will do that ....obviously! oops lol

Its on a product details page, and i want to retain values such as product
id, only for that page. So there is something in Page i can use? Could you
give me an example of how you would in such a situation. No need for full
code, just a line would do (assuming its that simple).










- Show quoted text -

Mark Rae is absolutely right. For variables with page scope, use the
ViewState. For variables with session scope, use the Session object.

Mike
 
Back
Top