使用javascript将值从一个列表框移动到另一个列表框然后使用c#读取值 [英] move value from one listbox to other using javascript and then read value using c#

查看:126
本文介绍了使用javascript将值从一个列表框移动到另一个列表框然后使用c#读取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

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>





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



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.

推荐答案

ListBox是一个服务器控件,它失去了从 javascript更改的值 on postback。

如果要保存值,可以使用隐藏控件,您可以在客户端和服务器端访问它们。

IMO,例如每当您向ListBox2添加项时,只需向隐藏字段添加值(以逗号分隔)。在页面回发中使用此隐藏字段来访问附加值。
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.


在客户端进行的更改仅在回发后反映服务器端,即如果表单提交这些列表框的提交。由于这听起来像某种多选屏幕,您可能希望通过下一步按钮或类似按钮进行,请确保列表框位于该按钮为提交控件的表单内。



如果它应该立即反馈,你需要将数据传递给服务器,可能是使用AJAX,以及在客户端移动项目。



顺便说一句,这就是我讨厌旧式ASP / ASP.net的原因。它模糊了服务器和客户端之间的界限,所以像你这样的人会对数据实际生活的位置以及在服务器或客户端上可以合理地做什么感到困惑。
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.


你可能想看看这个。我曾写过关于同一主题的博客,并且对我来说效果很好。如果您愿意,请检查链接。 :)



使用JavaScript在ListBoxes之间移动项目 [ ^ ]
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[^]


这篇关于使用javascript将值从一个列表框移动到另一个列表框然后使用c#读取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆