Losing the State of an ASP Listbox after postback for UpdatePanel

Discussion in 'ASP .Net' started by John Kotuby, Jan 22, 2008.

  1. John Kotuby

    John Kotuby Guest

    Hi all,

    I have an ASP.NET 2.0 page that uses 6 controls all within an UpdatePanel so
    that only parts of the page are updated upon postbacks. maybe I have
    configured something wrong.

    When the page first loads, the listbox is empty. I am adding OPTION elements
    to the listbox using clientside javascript. I understand that this only
    changes the Document object model in memory and that the changes cannot be
    seen using View Source in the browser.

    I use a postback to refresh another control with new items which the user
    can then (supposedly) select from to add to the listbox. However, during any
    postback, the listbox comes back empty. I am being very careful to not run
    any of the initializing code that usually runs on first load of the page.
    That is why I am perplexed by the apparent loss of state.

    Is this occurring because I am loading the listbox using Javascript? Or
    would this happen even if the listbox was pre-loaded with items upon initial
    page load. I suppose that is easy enough for me to test.

    I am guessing it has something to do with the Update Panel that holds all
    the controls.

    I am thinking of capturing the InnnerHtml of the listbox and saving it to a
    hidden field and then using that to restore the state of the listbox between
    postbacks, but that seems rather cludgy as a workaround.

    Does anyone have a suggestion for me (besides finding another occupation)?
    Is there some attribute I must set in the update panel or the control
    itself?

    Thanks to all...
     
    John Kotuby, Jan 22, 2008
    #1
    1. Advertising

  2. John Kotuby

    bruce barker Guest

    when an update panel postsbacks, its innerhtml is replaced by the
    server's response. you should pass the option info in a hidden field, so
    the server can reconstruct the list.

    -- bruce (sqlwork.com)

    John Kotuby wrote:
    > Hi all,
    >
    > I have an ASP.NET 2.0 page that uses 6 controls all within an UpdatePanel so
    > that only parts of the page are updated upon postbacks. maybe I have
    > configured something wrong.
    >
    > When the page first loads, the listbox is empty. I am adding OPTION elements
    > to the listbox using clientside javascript. I understand that this only
    > changes the Document object model in memory and that the changes cannot be
    > seen using View Source in the browser.
    >
    > I use a postback to refresh another control with new items which the user
    > can then (supposedly) select from to add to the listbox. However, during any
    > postback, the listbox comes back empty. I am being very careful to not run
    > any of the initializing code that usually runs on first load of the page.
    > That is why I am perplexed by the apparent loss of state.
    >
    > Is this occurring because I am loading the listbox using Javascript? Or
    > would this happen even if the listbox was pre-loaded with items upon initial
    > page load. I suppose that is easy enough for me to test.
    >
    > I am guessing it has something to do with the Update Panel that holds all
    > the controls.
    >
    > I am thinking of capturing the InnnerHtml of the listbox and saving it to a
    > hidden field and then using that to restore the state of the listbox between
    > postbacks, but that seems rather cludgy as a workaround.
    >
    > Does anyone have a suggestion for me (besides finding another occupation)?
    > Is there some attribute I must set in the update panel or the control
    > itself?
    >
    > Thanks to all...
    >
    >
     
    bruce barker, Jan 22, 2008
    #2
    1. Advertising

  3. John Kotuby

    John Kotuby Guest

    Thank's Bruce,

    Your insight is extremely helpful.
    Since I am constructing the OPTION elements with javascript I can at the
    same time save the Text portion of the ListItems in comma delimited fashion
    to one hidden field and the Value parts in a similar fashion to another
    field, then process the parallel arrays on the server.
    Of course that would preclude the presence of comma's in either the Text or
    Value portion.

    Sometimes it takes input from another brain to make the light go on.

    On the other hand, if I should save the entire clientside HTML contents in a
    hidden field I would have to figure a way to parse it.

    Unless a Javascript event fires after receipt of the panel update? Like
    Onload? Then if the HTML persists in the Hidden field I can just stuff it
    back into the control clientside after the callback completes.

    BTW... I found out if I add even one Option element at a time to the ListBox
    from the Server (during processing of the callback), the contents of the
    Listbox persists even after subsequent callbacks. So it appears that Option
    elements (ListItems) added (or removed) server-side persist through
    subsequent callbacks. Only if those changes are made via the DOM on the
    client-side do they Not persist. Interesting.

    Your help is much appreciated.

    "bruce barker" <> wrote in message
    news:...
    > when an update panel postsbacks, its innerhtml is replaced by the server's
    > response. you should pass the option info in a hidden field, so the server
    > can reconstruct the list.
    >
    > -- bruce (sqlwork.com)
    >
    > John Kotuby wrote:
    >> Hi all,
    >>
    >> I have an ASP.NET 2.0 page that uses 6 controls all within an UpdatePanel
    >> so that only parts of the page are updated upon postbacks. maybe I have
    >> configured something wrong.
    >>
    >> When the page first loads, the listbox is empty. I am adding OPTION
    >> elements to the listbox using clientside javascript. I understand that
    >> this only changes the Document object model in memory and that the
    >> changes cannot be seen using View Source in the browser.
    >>
    >> I use a postback to refresh another control with new items which the user
    >> can then (supposedly) select from to add to the listbox. However, during
    >> any postback, the listbox comes back empty. I am being very careful to
    >> not run any of the initializing code that usually runs on first load of
    >> the page. That is why I am perplexed by the apparent loss of state.
    >>
    >> Is this occurring because I am loading the listbox using Javascript? Or
    >> would this happen even if the listbox was pre-loaded with items upon
    >> initial page load. I suppose that is easy enough for me to test.
    >>
    >> I am guessing it has something to do with the Update Panel that holds all
    >> the controls.
    >>
    >> I am thinking of capturing the InnnerHtml of the listbox and saving it to
    >> a hidden field and then using that to restore the state of the listbox
    >> between postbacks, but that seems rather cludgy as a workaround.
    >>
    >> Does anyone have a suggestion for me (besides finding another
    >> occupation)? Is there some attribute I must set in the update panel or
    >> the control itself?
    >>
    >> Thanks to all...
     
    John Kotuby, Jan 23, 2008
    #3
    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. Vinay Joseph Mathew

    Losing Session State on a State Server

    Vinay Joseph Mathew, Aug 10, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    612
    =?Utf-8?B?c29ubnk=?=
    Sep 16, 2004
  2. Mel
    Replies:
    3
    Views:
    2,032
  3. Mel
    Replies:
    0
    Views:
    849
  4. Replies:
    2
    Views:
    553
  5. Replies:
    1
    Views:
    180
    Jorge L Matos
    Apr 22, 2005
Loading...

Share This Page