Fly

努力的飞翔!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Listbox的应用小技巧

Posted on 2008-04-24 17:00  nyflying  阅读(280)  评论(0)    收藏  举报
以前很少用Listbox,今天由于做项目要实现多选,但是做一个参照又做不出来,就只好笨人用笨方法啦,
实现,双击Listbox的内容,添加到一个Textbox内。但是Listbox又没有双击属性,那么我们就在后台给它添加一个双击属性。
首先:我们在后台为Listbox添加双击属性
    protected void Page_Load(object sender, EventArgs e)
    
{
        ListBox1.Attributes[
"onDblClick"= "change()";
    }
//为Listbox添加双击属性

然后双击值实现添加到文本框内(代码如下)
 1<head runat="server">
 2    <title>Listbox小技巧</title>
 3    <script type="text/javascript">
 4     function   change()   
 5            {   
 6            var   addOption=document.createElement("option")   
 7            var   index1   
 8            if(document.form1.ListBox1.length==0)return(false);   
 9              index1=document.form1.ListBox1.selectedIndex     
10            if(index1<0)return(false);   
11                
12              addOption.text=document.form1.ListBox1.options(index1).text   //获取选中的Listbox显示的值 
13              addOption.value=document.form1.ListBox1.value;  //获取选中的Listbox的数据库存储的值 
14              document.form1.Text1.value+=addOption.text+',';//这里我为添加的每个字段用‘,‘隔开  
15            }
   
16
17    </script>
18</head>
19<body>
20    <form id="form1" runat="server">
21    <div style="text-align: center">
22        <asp:ListBox ID="ListBox1" runat="server">
23            <asp:ListItem Value="one" Selected="True">1</asp:ListItem>
24            <asp:ListItem Value="two">2</asp:ListItem>
25            <asp:ListItem>3</asp:ListItem>
26        </asp:ListBox><asp:ListBox ID="ListBox2" runat="server"></asp:ListBox>
27        <input id="Text1" type="text" /></div>
28    </form>
29</body>

可是实现两个Listbox之间的传递
     function   change()   
            
{   
            var   addOption
=document.createElement("option")   
            var   index1   
            
if(document.form1.ListBox1.length==0)return(false);   
              index1
=document.form1.ListBox1.selectedIndex     
            
if(index1<0)return(false);   
                
              addOption.text
=document.form1.ListBox1.options(index1).text   //获取选中的Listbox显示的值 
              addOption.value=document.form1.ListBox1.value;  //获取选中的Listbox的数据库存储的值  
              document.form1.ListBox2.value.add(addOption); 
              document.form1.ListBox1.remove   (index1)   
            }
 
大概就是这样,一起学习!