Accessing List Items that were added to a list box via JavaScript

Discussion in 'ASP .Net' started by Scott324, Oct 27, 2005.

  1. Scott324

    Scott324 Guest

    Need some help guys:


    I have two list boxes that I am moving LI's back and forth. I
    initially fill listboxA with a datareader. I move any/all/some/etc
    items from listboxA to listboxB. I then cannot loop through items in
    listboxB on the server. It acts as if the items were never really
    added even though I can access them through JavaScript.

    Both the listboxes are server controls. I am not reinitializing the
    listboxes when trying to loop through.

    Is there a way via Javascript to make the server think the li's have
    been moved.


    One other side note. Serverside the count on listboxA never changes
    even if I move all items to listboxB.


    If there is no way to do it, can someone give a good thorough
    explanation of Why not!!!


    Thanks
    Scott
    Scott324, Oct 27, 2005
    #1
    1. Advertising

  2. Scott324

    Jason Kester Guest

    That's not how SELECT boxes work in CGI. The browser will submit a
    comma separated list of selected values. So if your user (or
    javascript) were to manually select all the items in ListBoxB before
    submitting the form, then they would be available to you in
    Request.Form["ListBoxB"], and possibly even the server representation
    of listboxB.

    If you want to keep a list of what's in box B, you'll need to do it by
    hand. Check out any battling listbox example out there, and that's how
    it is always done. You'll maintain a comma separated list of values in
    a hidden input, rebuilding that list in the same piece of script that
    you use to move the Option items from box to box.

    You should always try to learn what ASP.NET is doing for you behind the
    scenes, so that you don't come up against frustrating situations like
    this in the future. ASP.NET gives you the illusion that ListBoxes stay
    alive between trips to the server, but in reality it is only
    reconstituting those boxes from information that it stored in the
    VIEWSTATE the last time it was at the server. This works great in the
    simple case, where you're just selecting an item that's already in the
    list. But as you've learned, there's no way to tell the VIEWSTATE
    about changes to the Options colection.

    Good luck!

    Jason Kester
    Expat Software Consulting Services
    http://www.expatsoftware.com/

    ---
    Get your own Travel Blog, with itinerary maps and photos!
    http://www.blogabond.com/
    Jason Kester, Oct 27, 2005
    #2
    1. Advertising

  3. Scott324

    Scott324 Guest

    Jason,

    Thanks for the reply. Just to clear things up I am not concerned so
    much with the actual select process. Just when the Javascript moves a
    LI from A to B the server doesn't recognize the move. You explanation
    cleared it up though, the viewstate is not reflecting the changes made
    by javascript. Thanks, any way to update the viewstate?
    Scott324, Oct 27, 2005
    #3
  4. Scott324

    Jason Kester Guest

    Scott324 wrote:
    > any way to update the viewstate?


    Not really. Unless you want to decrypt it, modify it and re-encrypt it
    all from Javascript. It's probably technically doable, but I doubt
    anybody has tried.

    Jason
    Jason Kester, Oct 27, 2005
    #4
    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. Bart Van Hemelen
    Replies:
    2
    Views:
    4,654
    Bart Van Hemelen
    Sep 4, 2006
  2. Anjan Bhowmik
    Replies:
    1
    Views:
    461
    Misbah Arefin
    Feb 14, 2008
  3. Anthony Williams
    Replies:
    10
    Views:
    574
    Anthony Williams
    Sep 16, 2003
  4. Replies:
    7
    Views:
    82
    g4toloc0
    Feb 24, 2007
  5. Robert Kilroy
    Replies:
    3
    Views:
    93
Loading...

Share This Page