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

S

Scott324

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
 
J

Jason Kester

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/
 
S

Scott324

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?
 
J

Jason Kester

Scott324 said:
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
 

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

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top