首页 > 编程 > .NET > 正文

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

2024-07-10 13:27:03
字体:
来源:转载
供稿:网友
前台

复制代码 代码如下:


<head runat="server">
<title>部门多选</title>
<meta content="Microsoft Visual Studio.NET 7.0">
<meta content="C#">
<meta content="JavaScript">
<meta 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 method="post" runat="server">
<TABLE cellSpacing="0" cellPadding="0">
<TR>
<TD>
<INPUT type="hidden" value="<%=pass()%>">
<BUTTON type="button">确定</BUTTON>
<BUTTON type="button">取消</BUTTON>
<%-- <asp:Button runat="server" Text="确定" />--%>
</TD>
</TR>
<TR>
<TD vAlign="top">
<DIV>

<asp:listbox runat="server" SelectionMode="Multiple" DataTextField="Name" DataValueField="Name"></asp:listbox>
<asp:listbox runat="server"></asp:listbox>
<asp:label runat="server" CssClass="tttable">选取部门:</asp:label>
<asp:label runat="server" CssClass="tttable">已选定部门:</asp:label>
<asp:button runat="server" CssClass="bsbttn" Text="添加->"></asp:button>
<asp:button runat="server" CssClass="bsbttn" Text="<-删除"></asp:button>
<asp:button runat="server" CssClass="bsbttn" Text="全选"></asp:button>
<asp:button runat="server" CssClass="bsbttn" Text="全不选"></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;
}
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表