欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net listbox实现单选全选取消

程序员文章站 2024-02-24 14:26:40
前台 复制代码 代码如下: 部门多选
前台
复制代码 代码如下:

<head runat="server">
<title>部门多选</title>
<meta name="generator" content="microsoft visual studio.net 7.0">
<meta name="code_language" content="c#">
<meta name="vs_defaultclientscript" content="javascript">
<meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript" type="text/javascript">
function ok()
{
window.close();
opener.document.all.txtotherman.value = window.form2.hide.value;
}
function cancel()
{
window.close();
}
</script>
</head>
<body>
<form id="form2" method="post" runat="server">
<table style="width: 600px; position: absolute; height: 320px" cellspacing="0" cellpadding="0" border="0">
<tr>
<td style="width: 600px; height: 20px">
<input id="hide" type="hidden" value="<%=pass()%>">
<button id="ok" onclick="javascript:ok();" type="button">确定</button>
<button id="cancel" onclick="javascript:cancel();" type="button">取消</button>
<%-- <asp:button id="button1" runat="server" text="确定" onclick="button1_click" />--%>
</td>
</tr>
<tr>
<td style="width: 600px; height: 310px;" valign="top" align="left">
<div class="box" style="width: 600px ;height: 300px; left: 0px; top: 0px;">

<asp:listbox id="userlist" style="z-index: 105; left: 27px; position: absolute; top: 56px" runat="server" height="201px" width="200px" selectionmode="multiple" datatextfield="name" datavaluefield="name"></asp:listbox>
<asp:listbox id="lstselemp" style="z-index: 106; left: 317px; position: absolute; top: 58px" runat="server" height="202px" width="211px"></asp:listbox>
<asp:label id="label4" style="z-index: 107; left: 30px; position: absolute; top: 32px" runat="server" cssclass="tttable">选取部门:</asp:label>
<asp:label id="label5" style="z-index: 108; left: 316px; position: absolute; top: 34px" runat="server" cssclass="tttable" height="18px" width="108px">已选定部门:</asp:label>
<asp:button id="cmdadd" style="z-index: 109; left: 235px; position: absolute; top: 145px" runat="server" cssclass="bsbttn" height="27px" width="63px" text="添加->" onclick="cmdadd_click"></asp:button>
<asp:button id="cmddel" style="z-index: 110; left: 237px; position: absolute; top: 184px" runat="server" cssclass="bsbttn" height="30px" width="60px" text="<-删除" onclick="cmddel_click"></asp:button>
<asp:button id="cmdaddall" style="z-index: 111; left: 238px; position: absolute; top: 64px" runat="server" cssclass="bsbttn" height="30px" width="60px" text="全选" onclick="cmdaddall_click"></asp:button>
<asp:button id="cmddelall" style="z-index: 112; left: 236px; position: absolute; top: 104px" runat="server" cssclass="bsbttn" height="30px" width="60px" text="全不选" onclick="cmddelall_click"></asp:button>
</div>
</td>
</tr>
</table>
</form>

</body>

后台
复制代码 代码如下:

using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using bll;
using model;
public partial class boarding_harmselroleemp : system.web.ui.page
{
protected void page_load(object sender, system.eventargs e)
{
if(!ispostback)
{

if (bll.emailuser.getemailuserbyid(session["username"].tostring()).usercompany.tostring() == "北京")//登录判断

{

//左侧listbox绑定部门
this.userlist.items.clear();
this.userlist.items.add(new listitem("总董事长办公室", "总董事长办公室"));
this.userlist.items.add(new listitem("财务", "财务"));
this.userlist.items.add(new listitem("市场", "市场"));
this.userlist.items.add(new listitem("行政", "行政"));
this.userlist.items.add(new listitem("it", "it"));
}

}
}
protected void cmdadd_click(object sender, system.eventargs e)//添加到右侧
{
bool exist=false;//判断在右侧是否存在,不存在则将左侧部门添加到右侧
foreach( listitem li in lstselemp.items)
{
if(li.value==userlist.selecteditem.value)
{
exist=true;
}
}
if(exist==false)
{
lstselemp.items.add(new listitem(userlist.selecteditem.text,userlist.selecteditem.value));
}
}
protected void cmddel_click(object sender, system.eventargs e)//取消右侧选择
{
lstselemp.items.remove(lstselemp.selecteditem);
}
protected void cmddelall_click(object sender, system.eventargs e)//全部取消右侧选择
{
for(int i=0;i<lstselemp.items.count; )
lstselemp.items.remove(lstselemp.items[i]);
}
protected void cmdaddall_click(object sender, system.eventargs e)//全部添加到右侧
{
foreach(listitem li in userlist.items)
{
bool exist=false;
foreach( listitem lised in lstselemp.items)
if(lised.value==li.value)
exist=true;
if(exist==false)
lstselemp.items.add(new listitem(li.text,li.value));
}
}
public string pass()
{
string txt="";
for(int j=0;j < lstselemp.items.count;j++)
{
txt+=lstselemp.items[j];
txt+="','";
}
return txt;
}
}