Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying listbox items...

Status
Not open for further replies.

vb89

MIS
Aug 21, 2008
47
US
Essentially, I have an ASP.net page where I load a record set server side and upload it into a listbox. I'm trying to do all the movement functionalities of the listbox items client side.

Specifically, I'm trying to figure out how to copy selected listbox items from one listbox to another -- on button click. I've searched for a while, but every example that I found moves the actual item into another listbox, I just want to copy the selected item to another listbox.

If someone can provide an example or pseudo code, I would greatly appreciate it.
 
It will be the same code as the moving items is, just leave out the lines of code that remove the selected item

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Well that doesn't seem to quite work,

here's the code to move items...
Code:
  function MovePlayers(lstPlayerSelect,lstPlayerQueue,action)
  //function fnMoveItems(lstbxFrom,lstbxTo)
{
 var varFromBox = document.all(lstPlayerSelect);
 var varToBox = document.all(lstPlayerQueue); 
 var varMove = action;

     if ((varFromBox != null) && (varToBox != null)) 
     { 
      if(varFromBox.length < 1) 
      {
       alert('There are no items in the source ListBox');
       return false;
      }
      if(varFromBox.options.selectedIndex == -1) // when no Item is selected the index will be -1

      {
       alert('Please select an Item to move');
       return false;
      }
      
      while (varFromBox.options.selectedIndex >= 0) 
      { 
       var newOption = new Option(); // Create a new instance of ListItem 

       newOption.text = varFromBox.options[varFromBox.options.selectedIndex].text; 
       newOption.value = varFromBox.options[varFromBox.options.selectedIndex].value; 
       varToBox.options[varToBox.length] = newOption; //Append the item in Target Listbox
       varFromBox.remove(varFromBox.options.selectedIndex); //Remove the item from Source Listbox 

    } 
 }
 return false; 
}

If i remove the varFromBox.remove line, the function does nothing/doesn't work when called.
 
Any errors that occur?

because in that code commenting outthis line
Code:
       varFromBox.remove(varFromBox.options.selectedIndex); //Remove the item from Source Listbox

Will stop the delete from happening.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top