Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

move value from one listbox to other using javascript and then read value using c#

i have two listbox(listbox 1 and listbox2).i have used following javscript code to move value from one listbox to other.

 <script language="javascript" type="text/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

like image 889
sp_m Avatar asked Jan 09 '12 12:01

sp_m


1 Answers

As Amar Palsapure stated in the comments, changes on clientside with javascript does not reflect on server side without some hacking on your part (add the values to hidden fields etc. have a look here), so you would not be able to see the changes server side. I assume the line ListBox2.Items.Count is server side.

It would be a lot better and easier for you if you do an ajax request and do it server side within an update panel.

like image 62
TBohnen.jnr Avatar answered Oct 12 '22 21:10

TBohnen.jnr