使用javascript将值从一个列表框移动到另一个列表框然后使用c#读取值

sha*_*rad 6 javascript c# asp.net listbox

我有两个列表框(列表框1和列表框2).i使用以下javscript代码将值从一个列表框移动到另一个列表框.

 <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>
Run Code Online (Sandbox Code Playgroud)

此代码将值从一个列表框移动到另一个列表框,但实际上当我尝试读取第二个列表框值时,其中一个值被复制,我无法读取这些值.当我检查它显示ListBox2.Items.Count为0

TBo*_*jnr 4

正如 Amar Palsapure 在评论中所述,如果您没有进行一些黑客攻击(将值添加到隐藏字段等,请查看此处),则使用 javascript 进行的客户端更改不会反映在服务器端,因此您将无法看到更改服务器端。我假设该线路ListBox2.Items.Count是服务器端的。

如果您执行 ajax 请求并在更新面板中的服务器端执行,这对您来说会更好、更容易。