re-ordering ListBox items

S

Stimp

I have a listbox of values that I populate from a database.

I want the user to be able to re-order the list
(by first selecting an item and then clicking 'up' or 'down' buttons)
and then save the list back to the database.

First of all, I implemented the re-ordering through client-side
javascript, which worked great (also it didn't require a postback every
time an item was re-ordered). For testing purposes, I'm outputting the
contents of the ListBox collection to a string before I start writing
back to the database...

To my disbelief, upon clicking the button to output the ListBox, it only
output the original order of Items (not the re-ordered version) and
reloaded the screen back to the original order.

Obviously this is because the ListBox collection is held in Viewstate
and the client-side changes are not overwriting these.

This leads to my first question: is there any way for the client-side
changes to be written to Viewstate (or written to the ListBox
collection)??

Second question:
Alternatively, I could create a comma-delimited string containing a list
of the item values in the new order.. but I've not idea how to send this
back to the server... any ideas?

Finally, as a last resort I've tried to do this using asp.net server
controls (using post-back to update the collection everytime an item is
moved). This is the least-desirable alternative, but leads to my third
question:
Is it possible to somehow change the order of items in the ListBox
collection? I've had no luck finding any links on google.

Any help on the above is much appreciated,
Peter
 
G

Guest

1. Sorry, don't know of one.

2. Try storing your string in a hidden field.

3. Agreed this would be an annoying solution for the user, but in a pinch
probably pretty easy: on the postback, take the value of selectedindex,
decide up/down based on the button, then swap text and value of the two
listitems affected, e.g. listbox.items(selectedindex).text =
listbox.items(selectedindex+1).text, etc.

hth,

Bill
 
S

Stimp

1. Sorry, don't know of one.

What about using the IPostBackDataHandler interface?
Apparently this tests if the current state is different to that in
Viewstate and, if so, will update the ViewState accordingly..

reading about it now...

http://www.netomatix.com/updowncontrol.aspx

Anybody here used this interface, or even if it's right for what I'm
trying to do?

Thanks,
Peter
 
S

Stimp

What about using the IPostBackDataHandler interface?
Apparently this tests if the current state is different to that in
Viewstate and, if so, will update the ViewState accordingly..

reading about it now...

http://www.netomatix.com/updowncontrol.aspx

Anybody here used this interface, or even if it's right for what I'm
trying to do?

I figured it out using a hidden field and Viewstate.

And no postback.. yay me :)

Thanks again for the help
 

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,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top