ListBox question

  • Thread starter Thread starter Scott Reynolds
  • Start date Start date
S

Scott Reynolds

Hello!

Could someone provide me a sample code how to move values between two
ListBox controls?

Thank you!
Scott Reynolds
 
Hi Scott,
Here is some javascript code to move values between two listboxes.
<script language="javascript">
//This function adds the values from the fromlistbox to the tolistbox
function Move(fromList,toList)
{
//If no items are selected then display an error message and return
if(fromList.selectedIndex == -1)
{
alert('Please select atleast one record');
return;
}
var selIndex = fromList.selectedIndex;
var optionText = fromList.options[selIndex].text;
var optionVal = fromList.options[selIndex].value;
var optionIndex = toList.options.length;
//decalre a new listobject and add it to the other listbox
var objOption = new Option(optionText, optionVal);
toList.options[optionIndex] = objOption;
fromList.options[selIndex] = null;
}
//Adds the values from the left listbox to the right listbox
function AddVals()
{
var lstFrom = document.getElementById("lstFrom");
var lstTo = document.getElementById("lstTo");
Move(lstFrom,lstTo)
}
//Removes the values from the right box and adds them back to the left box
function RemVals()
{
var lstFrom = document.getElementById("lstTo");
var lstTo = document.getElementById("lstFrom");
Move(lstFrom,lstTo)
}
</script>

if you want to do it in server side its pretty straightforward like
ListBox2.Items.add(ListBox1.SelectedItem)
HTH
srini
 
Thank you very much, your code works very well!

But what if I select all values in ListBox1 and want to move them all to
ListBox2?

Scott
Hi Scott,
Here is some javascript code to move values between two listboxes.
<script language="javascript">
//This function adds the values from the fromlistbox to the tolistbox
function Move(fromList,toList)
{
//If no items are selected then display an error message and return
if(fromList.selectedIndex == -1)
{
alert('Please select atleast one record');
return;
}
var selIndex = fromList.selectedIndex;
var optionText = fromList.options[selIndex].text;
var optionVal = fromList.options[selIndex].value;
var optionIndex = toList.options.length;
//decalre a new listobject and add it to the other listbox
var objOption = new Option(optionText, optionVal);
toList.options[optionIndex] = objOption;
fromList.options[selIndex] = null;
}
//Adds the values from the left listbox to the right listbox
function AddVals()
{
var lstFrom = document.getElementById("lstFrom");
var lstTo = document.getElementById("lstTo");
Move(lstFrom,lstTo)
}
//Removes the values from the right box and adds them back to the left box
function RemVals()
{
var lstFrom = document.getElementById("lstTo");
var lstTo = document.getElementById("lstFrom");
Move(lstFrom,lstTo)
}
</script>

if you want to do it in server side its pretty straightforward like
ListBox2.Items.add(ListBox1.SelectedItem)
HTH
srini

Scott Reynolds said:
Hello!

Could someone provide me a sample code how to move values between two
ListBox controls?

Thank you!
Scott Reynolds
 
I don't know about javascript but server-side, I do the following:

foreach (ListItem li in FromList) {
if (li.Selected == false)
continue;

ToList.Items.Add(li); // Add to beginning

or

ToList.Items.Insert(<index>, li); // Add at <index>

FromList.Items.Remove(li);
}

That may not be 100% correct as I'm doing it from memory but in general
that's how I do this.

James
 
Back
Top