zoukankan      html  css  js  c++  java
  • asp.net中的ListBox控件添加双击事件

    问题:在Aspx页里的ListBox A中添加双击事件,将选中项添加到另一个ListBox B中,双击ListBox B中的选中项,删除当前选中项

    页面:

      <asp:ListBox ID="ListUsers" runat="server" Height="313px" SelectionMode="Multiple" Width="185px" ></asp:ListBox>
    
      <asp:ListBox ID="ListSelectedUsers" runat="server" Height="313px"  SelectionMode="Multiple" Width="199px" "></asp:ListBox>

      <asp:TextBox ID="SelectedMode" runat="server" Style="display: none"></asp:TextBox>

    JS 脚本:

     function SelectOne() {
    
                var lst1 = window.document.getElementById("ListUsers");
                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("ListSelectedUsers");
                var mode = window.document.getElementById("SelectedMode");
                if (mode.value == "True") { //如果是单值,先删除所有
                    for (var i = lst2.options.length - 1; i >= 0; i--) {
                        lst2.remove(i);
                    }
                    lst2.add(new Option(t, v, true, true), 0);
                } else{
                    var isExists = false;
                    for (var i = 0; i < lst2.options.length; i++) {
                        if (lst2.options[i].value == v) { //防止添加重复项
                            isExists = true;
                            break;
                        }
                    }
                    if (!isExists) {
                        lst2.options[lst2.options.length] = new Option(t, v, true, true);
                    }
                }
            }
    
            //选择一条数据删除
            function DelOne() {
                var lst = window.document.getElementById("ListSelectedUsers");
                var lstindex = lst.selectedIndex;
                if (lstindex >= 0) {
                    var v = lst.options[lstindex].value + ";";
                    lst.options[lstindex].parentNode.removeChild(lst.options[lstindex]);
                }
            }

    后台代码:

    if (!IsPostBack)
     {
        ListBoxBand();
        SelectedMode.Text = "True";
        ListUsers.Attributes.Add("onDblClick", "SelectOne()");//双击事件    
    ListSelectedUsers.Attributes.Add("onDblClick", "DelOne()");
    //双击事件 } private void ListBoxBand() { for (int i = 0; i < 10; i++) {    ListUsers.Items.Add(new ListItem("Index" + i, i.ToString())); } }

    效果:

    参考来源:asp.net中的ListBox控件添加双击事件

    js操作ListBox实现多项的添加和删除

  • 相关阅读:
    Linux查看文件夹大小
    mysql按照天或小时group分组统计
    eclipse可以调试但是无法打开网页,提示一直在加载
    自定义spring valid方式实现验证
    UniCode编码表及部分不可见字符过滤方案
    shiro中移除jsessionid的解决方案
    Apache Shiro去掉URL中的JSESSIONID
    shiro开启realm
    shiro注解@RequiresPermissions多权限任选一参数用法
    linux 复制粘贴
  • 原文地址:https://www.cnblogs.com/cpcpc/p/4992274.html
Copyright © 2011-2022 走看看