asp.net中的ListBox控件添加双击事件
2015-11-24 17:28
323 查看
原文链接:https://www.geek-share.com/detail/2659357284.html
问题:在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())); } }
效果:
转载于:https://www.cnblogs.com/cpcpc/p/4992274.html
相关文章推荐
- asp.net中的ListBox控件添加双击事件
- asp.net中的ListBox控件添加双击事件
- asp.net ListBox控件添加双击事件
- asp.net中为Calendar日历控件添加双击日期事件
- asp.net 动态添加checkbox与事件
- asp.net控件中怎么添加onfocus和onblure等事件?
- 以编程方式向 ASP.NET 控件添加客户端事件处理程序--TextBox.Attributes.Add()
- ASP.NET DEMO15_1: GridView 行单击与双击事件2
- ASP.NET DEMO 15: 同时支持行单击和双击事件的 GridView/DataGrid
- 向 ASP.NET Web 服务器控件添加客户端脚本事件
- asp.net(C#)中给控件添加客户端js事件的方法
- ASP.NET:为 AJAX 请求添加客户端事件
- 为.net中的ListBox控件添加双击事件-.NET教程,组件控件开发
- ASP.NET页面中动态增加的控件、添加事件
- asp.net页面动态添加控件造成控件事件不响应的解决办法
- 为.net中的ListBox控件添加双击事件
- 为.net中的ListBox控件添加双击事件
- ASP.NET自定义控件组件开发 第三章 为控件添加事件 前篇
- ASP.NET页面中动态增加的控件、添加事件
- ASP.NET:为 AJAX 请求添加客户端事件