Retaining values after a postback

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
 
M

Mike Hofer

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
 
P

PokerMan

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).
 
M

Mark Rae

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.
 
P

PokerMan

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!
 
M

Mike Hofer

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
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top