asp.net中的ListBox控件添加双击事件

问题:通过在一个ListBox中双击,把选中的项添加到另一个ListBox中,但ListBox控件本身并没有该事件,那么如何实现呢?客户端脚本javascript可以帮忙。

步骤:1.写脚本

 <script type="text/javascript">
        function change(){     
            var addOption=document.createElement("option");     
            var index1;     
            if(document.form1.list_Client.length==0)
                return(false);     
            index1=document.form1.list_Client.selectedIndex;      
            if(index1<0)
                return(false);     
            addOption.text=document.form1.list_Client.options(index1).text;     
            addOption.value=document.form1.list_Client.value;    
            document.form1.list_Client2.add(addOption);     
            document.form1.list_Client.remove (index1);                       
     }
</script>

2.页面添加两个ListBox 以做测试

<body>
    <form id="form1" runat="server">
       <asp:ListBox ID="list_Client" runat="server" Width="240" Height="300"></asp:ListBox>

   >>
       <asp:ListBox ID="list_Client2" runat="server" Width="240" Height="300"></asp:ListBox>
    </form>
</body>

3.后台页面加载事件中注册。

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ListBoxBand();
            list_Client.Attributes.Add("onDblClick", "change()");//双击事件

    //list_Client.Attributes.Add("onClick", "change()");//单击事件
        }
    }

测试结果:

附js操作ListBox方法:

  //一次选择list_Client所有数据导入list_Client2中

  function SelectAll(){

            var lst1=window.document.getElementById("list_Client");
            var length = lst1.options.length;
            for(var i=0;i <length;i++) {
                var v = lst1.options[i].value;
                var t = lst1.options[i].text;
                var lst2=window.document.getElementById("list_Client2");
                lst2.options[i] = new Option(t,v,true,true);   
            } 
        }

  //一次选择删除list_Client中所有数据

   function DelAll(){

            var lst=window.document.getElementById("list_Client");
            var length = lst.options.length;
            for(var i=length;i>0;i--) {
                lst.options[i-1].parentNode.removeChild(lst.options[i-1]);
            }   
        }

   //选择一条数据添加到另一个ListBox,步骤详见上面的例子

     function SelectOne(){

            var lst1=window.document.getElementById("list_Client");
            var lstindex=lst1.selectedIndex;
            if(lstindex<0)
                return;
            var v = lst1.options[lstindex].value;
            var t = lst1.options[lstindex].text;
            var lst2=window.document.getElementById("list_Client2");
            lst2.options[lst2.options.length] = new Option(t,v,true,true);    
        }

 

   //选择一条数据删除

  function DelOne(){
            var lst=window.document.getElementById("list_Client");
            var lstindex=lst.selectedIndex;
            if(lstindex>=0) {
                var v = lst.options[lstindex].value+";";
                lst.options[lstindex].parentNode.removeChild(lst.options[lstindex]);
            }

        }

posted @ 2012-03-06 11:05  烁星  阅读(4436)  评论(1编辑  收藏  举报