您的位置:首页 > 编程语言 > ASP

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())); } }

效果:

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

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

 

转载于:https://www.cnblogs.com/cpcpc/p/4992274.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: