ViewState Question

J

JN

When the button click event is raised, the value of the selectedindex of
the listbox is stored in the viewstate.

After refreshing the page using response.redirect, I want the listbox
selectedindex to be the value in the viewstate but value is always 0
instead.

Why is that?

Thanks.
JN
 
G

Guest

ViewState only persists between postbacks to the same page.
Response.Redirect is not a postback.
If you want the value to persists between different pages you'll probably
want to use Session or Application state instead of ViewState.
If you're intending to stay on the same page then why are you using
reponse.redirect?
 
J

JN

Thanks! Session state did the trick.

If I wanted to stay on the same page, how would I refresh it?
 
T

Teemu Keiski

Hi,

redirect is the same as coming to completely refreshed page (e.g starting
from 0). ViewState is persisted only on postbacks. eg when you make a POST
request to the page with posted data (also having the viewstate hidden
field's data). Redirect does not post, so there's not any viewstate involved
and controls cannot keep their state. If you cannot make it as a postback
instead of redirecting, you could use session variables to store the index
and restore on after redirecting.

See: Understanding ASP.NET ViewState
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/viewstate.asp
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top