views:

231

answers:

3

I reordered some items in a listbox using Javascript. When I read the items in a postback in the code behind (ASP.NET), the order is in the original order. How do I get the same order as shown in the screen after Javascript manipulation?

A: 

Your JavaScript will have to notify the server of the rearrangement using AJAX, and then you have to keep track of order manually. This is fairly trivial, but it does mean regenerating the listbox items each page load instead of relying on the ViewState.

David Pfeffer
I opted to postback every time the user reordered an item and do the reordering in the code behind. It's a simple back and the postback is very quick.
Tony_Henrich
If you put that component into an UpdatePanel and hook up the AsyncPostBackTrigger to the postback-generating event (altogether a trivial 3-4 line HTML change), ASP.NET will invisibly transform this postback into an AJAX update.
David Pfeffer
+1  A: 

Only the selected items will be posted back to the server, the order will be pulled from the viewstate (which your javascript doesnt change). Im not sure you can do what you want in that way. You might have to have a separate [hidden] field that tells the server what order things are in.

John Boker
A: 

Hey,

Using JavaScript code, you can store the order in a hidden variable (make the hidden variable the server HiddenField variable) and process the ordering when the page posts back to the server. Then, you can clear the items and reorder them appropriately using this sequence of numbers stored in the hiddenfield. This is what the AJAX controls do.

Brian