ListBox problem - by design?

Discussion in 'ASP .Net Web Controls' started by Geoff Pennington, Apr 27, 2004.

  1. I have a page with two WebControls.ListBox controls. The first listbox is
    for unassigned staff, the second is for assigned staff. I use client-side
    JavaScript to let the user move items back and forth between the two lists.
    The client side code works fine.

    When the user submits the form I want to update the database from the
    current contents of the "Assigned" list. Problem is, my server side code
    only "sees" the original content of the datalists. For example, if there are
    four names in each list and I move one from "Unassigned" to "Assigned", the
    server side code thinks there are still four items in each list. (I checked
    that using the debugger.) When the browser window redisplays, the lists
    appear as they did before the client side re-arranging. This is with
    ViewState enabled; if I disable ViewState the lists come up blank.

    Is there a way to make the server code see the results of the client side
    activity? The best I've come up with is to let the client side code maintain
    a list in a
    hidden field of who is in which listbox, and letting the server code use
    that list, but that seems kludgy. I'm wondering if, since the ListBox
    contents can't be directly edited, there simply is no way to post "current
    contents" back to the server.

    Much obliged.
    Geoff Pennington, Apr 27, 2004
    #1
    1. Advertising

  2. Hi Geoff,

    Thats by design. Any changes to the items that you may perform on the
    client-side won't be automatically picked up by the server. You will need
    write some code to post the modified lists, parse them early at the server
    and update each listbox items accordinly which is what you were already
    doing I believe.

    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    http://obies.com/vga/blog.aspx
    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup

    "Geoff Pennington" <!SPAM!> wrote in message
    news:...
    > I have a page with two WebControls.ListBox controls. The first listbox is
    > for unassigned staff, the second is for assigned staff. I use client-side
    > JavaScript to let the user move items back and forth between the two

    lists.
    > The client side code works fine.
    >
    > When the user submits the form I want to update the database from the
    > current contents of the "Assigned" list. Problem is, my server side code
    > only "sees" the original content of the datalists. For example, if there

    are
    > four names in each list and I move one from "Unassigned" to "Assigned",

    the
    > server side code thinks there are still four items in each list. (I

    checked
    > that using the debugger.) When the browser window redisplays, the lists
    > appear as they did before the client side re-arranging. This is with
    > ViewState enabled; if I disable ViewState the lists come up blank.
    >
    > Is there a way to make the server code see the results of the client side
    > activity? The best I've come up with is to let the client side code

    maintain
    > a list in a
    > hidden field of who is in which listbox, and letting the server code use
    > that list, but that seems kludgy. I'm wondering if, since the ListBox
    > contents can't be directly edited, there simply is no way to post "current
    > contents" back to the server.
    >
    > Much obliged.
    >
    >
    Victor Garcia Aprea [MVP], Apr 28, 2004
    #2
    1. Advertising

  3. Geoff Pennington

    David Jessee Guest

    Remember, at the client level, we're looking at good old-fashioned HTML.
    An HTML SELECT element only posts the selected item(s) in the control. It
    diesn't post its content to the server. So, you have a couple of options
    here.

    1) You can create a script that fires on the form's "onsubmit" event which
    goes through the listbox and selects all fo the items. Then, the listbox
    will post everything back to the server and when the control's viewstate is
    put back together, it will have all of its items selected.

    2) (This is a less elegant options, but it has more uses in similar
    scenarios) You create an html Hidden Field that runs at the server. As the
    listbox's content changes, you have a script modify the content of that
    hidden field appropriately (for example, it might be a comma-delimited list
    of selected items). When the postback occurs, you can infer the state of
    that control by what's in the hidden field. This is a GREAT way to handle
    some types of state transitions at the client level that might not be
    supported in the standard posting mechanisim of the HTML standard.

    (dang....that got wordier than I though)

    "Geoff Pennington" <!SPAM!> wrote in message
    news:...
    > I have a page with two WebControls.ListBox controls. The first listbox is
    > for unassigned staff, the second is for assigned staff. I use client-side
    > JavaScript to let the user move items back and forth between the two

    lists.
    > The client side code works fine.
    >
    > When the user submits the form I want to update the database from the
    > current contents of the "Assigned" list. Problem is, my server side code
    > only "sees" the original content of the datalists. For example, if there

    are
    > four names in each list and I move one from "Unassigned" to "Assigned",

    the
    > server side code thinks there are still four items in each list. (I

    checked
    > that using the debugger.) When the browser window redisplays, the lists
    > appear as they did before the client side re-arranging. This is with
    > ViewState enabled; if I disable ViewState the lists come up blank.
    >
    > Is there a way to make the server code see the results of the client side
    > activity? The best I've come up with is to let the client side code

    maintain
    > a list in a
    > hidden field of who is in which listbox, and letting the server code use
    > that list, but that seems kludgy. I'm wondering if, since the ListBox
    > contents can't be directly edited, there simply is no way to post "current
    > contents" back to the server.
    >
    > Much obliged.
    >
    >
    David Jessee, Apr 28, 2004
    #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. blenderdude
    Replies:
    0
    Views:
    969
    blenderdude
    Aug 3, 2003
  2. DC Gringo
    Replies:
    0
    Views:
    398
    DC Gringo
    Apr 6, 2004
  3. kent
    Replies:
    1
    Views:
    875
    Andrew de la Harpe
    May 3, 2004
  4. Simon Prince
    Replies:
    2
    Views:
    3,320
    =?Utf-8?B?TWljaGFlbCBUYXlsb3I=?=
    Oct 19, 2004
  5. Chris Kettenbach

    Listbox selection to populate another listbox?

    Chris Kettenbach, Jun 15, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    1,644
    Chris Kettenbach
    Jun 16, 2005
Loading...

Share This Page