最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

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

Share Improve this question edited Jul 10, 2012 at 13:02 sp_m asked Jan 9, 2012 at 12:47 sp_msp_m 2,7058 gold badges40 silver badges66 bronze badges 3
  • 8 Not sure but, there is View State associated with asp controls, as you are adding items on client side, it might not reflect on server. Anyone please correct if I'm wrong. – Amar Palsapure Commented Jan 9, 2012 at 12:53
  • Maybe this can help : extendedlistbox.codeplex.. It replace on the fly an html listbox with two drop down list, with buttons to move between the twos. From a server side, it's easy to manipulate as you works with a simple listbox with multiple selection. Disclaimer I'm the creator of the project – Steve B Commented Jan 9, 2012 at 13:03
  • Here's one way of doing it: dotnet-developer.de/2008/07/aspnet2005/ajax/… – keyboardP Commented Jan 9, 2012 at 13:21
Add a ment  | 

4 Answers 4

Reset to default 4

As Amar Palsapure stated in the ments, 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.

Make sure that on the page load you are not over writing the values by putting the listbox setup code in an if statement making sure it is not a postback.

The server side process (C) is not able to read from the client, without an HTTP request - page reload(which is probably not what you want to do). Your javascript looks good, but you probably need to use AJAX technique, which allows your client code to speak to your server code without reloading the page in the traditional HTTP request model.

Try using JQuery library to assist in setting up your request to the server. http://api.jquery./jQuery.ajax/

As TBohnen.jnr suggested I remend using an Update Panel and then an Asynchrounous Postback Trigger to refresh the update panel. You will need to put the list boxes inside the panel and then have an event that you raise when the content is moved.

发布评论

评论列表(0)

  1. 暂无评论