ViewState Question

Discussion in 'ASP .Net' started by JN, Feb 15, 2006.

  1. JN

    JN Guest

    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
    JN, Feb 15, 2006
    #1
    1. Advertising

  2. 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?

    --
    I hope this helps,
    Steve C. Orr
    MCSD, MVP
    http://Steve.Orr.net



    "JN" wrote:

    > 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
    >
    >
    >
    >
    =?Utf-8?B?U3RldmUgQy4gT3Jy?=, Feb 15, 2006
    #2
    1. Advertising

  3. JN

    JN Guest

    Thanks! Session state did the trick.

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

    "Steve C. Orr" <> wrote in message
    news:...
    > 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?
    >
    > --
    > I hope this helps,
    > Steve C. Orr
    > MCSD, MVP
    > http://Steve.Orr.net
    >
    >
    >
    > "JN" wrote:
    >
    >> 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
    >>
    >>
    >>
    >>
    JN, Feb 15, 2006
    #3
  4. JN

    Teemu Keiski Guest

    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

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke

    "JN" <> wrote in message
    news:...
    > 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
    >
    >
    >
    Teemu Keiski, Feb 15, 2006
    #4
  5. I'd probably use a little javascript to submit the form so it posts back.

    --
    I hope this helps,
    Steve C. Orr
    MCSD, MVP
    http://Steve.Orr.net



    "JN" wrote:

    > Thanks! Session state did the trick.
    >
    > If I wanted to stay on the same page, how would I refresh it?
    >
    > "Steve C. Orr" <> wrote in message
    > news:...
    > > 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?
    > >
    > > --
    > > I hope this helps,
    > > Steve C. Orr
    > > MCSD, MVP
    > > http://Steve.Orr.net
    > >
    > >
    > >
    > > "JN" wrote:
    > >
    > >> 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
    > >>
    > >>
    > >>
    > >>

    >
    >
    >
    =?Utf-8?B?U3RldmUgQy4gT3Jy?=, Feb 15, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Rush
    Replies:
    2
    Views:
    522
    Ben Rush
    Dec 5, 2003
  2. Steph
    Replies:
    2
    Views:
    1,660
    heislord5
    May 11, 2011
  3. ujjc001
    Replies:
    0
    Views:
    1,370
    ujjc001
    Jul 27, 2005
  4. sck10
    Replies:
    6
    Views:
    913
    sck10
    Sep 1, 2006
  5. mark

    Viewstate errors... how do I get viewstate working?

    mark, Feb 20, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    276
Loading...

Share This Page