昨天在做公司的小项目时又用到了ListBox,我几近抓狂,大家可能都知道的,vs.net2002和2003版本的ListBox控件尽管有个selectionmode属性,设为"mutiple"时确实可以选中多个项,可是在后台取值时却只能取得一个值,够郁闷吧。史特(shit)大人,mutiple究竟有什么实际的用处阿。。。
我只能考虑Javascript了,思路如下:
用两个listbox,双击其中一个的项将此项选到另一个listbox中去,后台取值时根据后者中的项。
为listbox的ondblclick事件添加javascript代码如下:
listbox ondblclick代码
1function lbx_change()
2{
3 var cid=event.srcElement.id;
4 var addOption=document.createElement("option");
5 var index1;
6 switch(cid)
7 {
8 case "lbx_orgList":
9 if(document.all.lbx_orgList.length==0)return(false);
10 index1=document.all.lbx_orgList.selectedIndex;
11 if(index1<0)return(false);
12 addOption.text=document.all.lbx_orgList.options(index1).text;
13 addOption.value=document.all.lbx_orgList.value ;
14 document.all.lbx_sorgList.add(addOption);
15 document.all.lbx_orgList.remove(index1) ;
16 break;
17 case "lbx_sorgList":
18 if(document.all.lbx_sorgList.length==0)return(false);
19 index1=document.all.lbx_sorgList.selectedIndex;
20 if(index1<0)return(false);
21
22 addOption.text=document.all.lbx_sorgList.options(index1).text;
23 addOption.value=document.all.lbx_sorgList.value;
24 document.all.lbx_orgList.add(addOption);
25 document.all.lbx_sorgList.remove(index1) ;
26 break;
27 }
28}
1function lbx_change()
2{
3 var cid=event.srcElement.id;
4 var addOption=document.createElement("option");
5 var index1;
6 switch(cid)
7 {
8 case "lbx_orgList":
9 if(document.all.lbx_orgList.length==0)return(false);
10 index1=document.all.lbx_orgList.selectedIndex;
11 if(index1<0)return(false);
12 addOption.text=document.all.lbx_orgList.options(index1).text;
13 addOption.value=document.all.lbx_orgList.value ;
14 document.all.lbx_sorgList.add(addOption);
15 document.all.lbx_orgList.remove(index1) ;
16 break;
17 case "lbx_sorgList":
18 if(document.all.lbx_sorgList.length==0)return(false);
19 index1=document.all.lbx_sorgList.selectedIndex;
20 if(index1<0)return(false);
21
22 addOption.text=document.all.lbx_sorgList.options(index1).text;
23 addOption.value=document.all.lbx_sorgList.value;
24 document.all.lbx_orgList.add(addOption);
25 document.all.lbx_sorgList.remove(index1) ;
26 break;
27 }
28}
做到这出了个新的问题,从后台竟然取不到listbox(上例中为lbx_sorgList)中选好的值,lbx_sorgList.Items.count为0。我想了下原因,估计是因为lbx_sorgList中的项是由javascript在客户端添加的,后台代码不认帐。我试了下用文本框同步保存lbx_orgList双击选中到lbx_sorgList中的项,后台经过字符串处理后可以得到这些项的值,可是这样做却绕了一个圈子。另外一个方法是用JavaScript取lbx_sorgList中的值,这是我接下来要解决的问题。