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實現多項的添加和刪除

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM