Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two listbox(listbox 1 and listbox2).i have used following javscript code to move value from one listbox to other.

JavaScript
function fnMoveItems(lstbxFrom,lstbxTo)
{
 var varFromBox = document.all(lstbxFrom);
 var varToBox = document.all(lstbxTo); 
 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; 
}
</script>


This code moves value from one listbox to another,but actually when i try to read the second listbox values, one to whhich values are copied , i am not able to read those values. when i check it shows ListBox2.Items.Count is 0.
Posted
Updated 9-Jan-12 2:00am
v2

ListBox is a server control,it loses value changed from javascript on postback.
If you want to save the value,you can use hidden controls which you can access in both client as well as server side.
IMO, e.g. Whenever you are adding item to ListBox2 just add value to hidden field (comma separated). In page postback use this hidden field to access added value.
 
Share this answer
 
Changes made on the client side will only be reflected server side after a postback, i.e. if the form in which these list boxes are present is submitted. Since this sounds like some sort of multi-select screen from which you probably want to progress through a 'Next' button or similar, make sure the list boxes are inside the form for which that button is a submit control.

If it's supposed to be fed back instantly, you need to pass the data to the server, probably with AJAX, as well as moving the items on the client side.

As an aside, this is why I hate old style ASP/ASP.net. It blurs the boundaries between server and client so people such as yourself get confused about where the data actually 'lives', and what can reasonably be done on the server or client.
 
Share this answer
 
You might want to have a look at this. I had blogged about the same topic and it's working pretty well for me. Check the link if you want to. :)

Moving items between ListBoxes using JavaScript[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900