• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

问题:通过在一个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 ></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]);
            }

        }


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap