ListBox problem - by design?

G

Geoff Pennington

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.
 
V

Victor Garcia Aprea [MVP]

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
 
D

David Jessee

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)
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top