Moving listbox item from listbox to listbox

K

Kevin Quigley

Hi,

I have an application that allows users to select items from one listbox
and in doing so the item is removed from the original list and displayed
in the other.

I had this working in the code behind page but each selection resulted
in a postback. I have now tried to implement this using javascipt and
the transferring of items works fine, but when I try access the items
that the user has selected (in the onClick event of a button) they are
not available. Does anyone know how I can solve this?

Here is the javascript I use to transfer items between listboxes.

function transferOption()
{
var object = document.Form1;
var index = object.ListBox1.selectedIndex;
if (index > -1)
{
var newoption = new Option(object.ListBox1.options[index].text,
object.ListBox1.options[index].value, true, true);
object.ListBox2.options[object.ListBox2.length] = newoption;
if (!document.getElementById) history.go(0);
object.ListBox1.options[index] = null;
object.ListBox1.selectedIndex = 0;
}
}

Any help on this would be gratefully appreciated.

Thanks,
Kevin
 
A

AMerrell

Kevin,

I built a control similar to this. What I did was added a javascript function that would transfer all the ItemValues and ItemText to Hidden Input Fields delimited with pipes.

I would call this function each time I move an item from one listbox to the other.

Then in my control I would use findcontrol to get the information our of the Hidden Input Fields and load them into arrays.
Loop through each element in the arrays and add them to a ListItemCollection.

Hope this helps!

AMerrell
 
K

Kevin Quigley

Thanks for that, I've now put it in a control too and it works quite
well.

Thanks again.
 

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

Top