前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UserManager.aspx.cs" Inherits="Admin_Account_UserManager" %>
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
<%@ Register Src="http://www.cnblogs.com/Controls/UserAdd.ascx" TagName="UserAdd" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="http://www.cnblogs.com/FloatDiv/FloatDiv.js"></script>
<link rel="Stylesheet" href="http://www.cnblogs.com/FloatDiv/FloatDiv.css" />
<script type="text/javascript">
function ConfirmDel() {
return confirm("确定删除吗?");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div style="margin: outo">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table>
<tr>
<td>
<table>
<tr>
<td>
<asp:CheckBox ID="CheckBoxSelectAll" runat="server" Text="全选" AutoPostBack="True"
OnCheckedChanged="CheckBoxSelectAll_CheckedChanged" />
| 
<asp:Button ID="ButtonDel" runat="server" Text="删除" OnClientClick="javascript:return ConfirmDel()"
OnClick="ButtonDel_Click" /> | 
<asp:Button ID="ButtonAdd" runat="server" Text="添加" OnClientClick="ShowFloatDiv('bgDiv','FloatDivAdd')"
OnClick="ButtonAdd_Click" />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<asp:DataList ID="DataListUserManager" runat="server" Font-Size="12px" Width="800px"
OnCancelCommand="DataListUserManager_CancelCommand" OnDeleteCommand="DataListUserManager_DeleteCommand"
OnEditCommand="DataListUserManager_EditCommand" OnUpdateCommand="DataListUserManager_UpdateCommand"
BorderWidth="1px">
<HeaderTemplate>
<table style="border-bottom-style: inset; border- 1px; border-color: #C0C0C0"
width="100%">
<tr>
<td width="60">
选择
</td>
<td width="100">
用户名
</td>
<td width="100">
真实姓名
</td>
<td width="60">
性别
</td>
<td width="120">
电话
</td>
<td width="120">
邮箱
</td>
<td width="60">
激活
</td>
<td width="80">
编辑
</td>
<td width="60">
删除
</td>
</tr>
</table>
</HeaderTemplate>
<SeparatorTemplate>
<div style="border- 1px; border-color: #C0C0C0; border-style: dashed none none none;
line-height: 2px; 100%; font-family: 'Arial Unicode MS'; font-size: xx-small;">
</div>
</SeparatorTemplate>
<ItemTemplate>
<table width="100%">
<tr>
<td width="60">
<asp:CheckBox ID="CheckBoxSelect" runat="server" />
</td>
<td width="100">
<asp:Literal ID="UserID" Text='<%#Eval("UserID")%>' Visible="false" runat="server"></asp:Literal>
<%#Eval("UserName")%>
</td>
<td width="100">
<%#Eval("TrueName")%>
</td>
<td width="60">
<%#Eval("Sex")%>
</td>
<td width="120">
<%#Eval("Phone")%>
</td>
<td width="120">
<%#Eval("Email")%>
</td>
<td width="60">
<%#bool.Parse(Eval("Activity").ToString())==true?"是":"否"%>
</td>
<td width="80">
<asp:LinkButton ID="LinkButtonEdit" Text="编辑" CommandName="Edit" runat="server"></asp:LinkButton>
</td>
<td width="60">
<asp:LinkButton ID="LinkButtonDelete" Text="删除" CommandName="Delete" runat="server"
OnClientClick="javascript:return ConfirmDel()"></asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
<EditItemTemplate>
<table width="100%">
<tr>
<td width="60">
<asp:CheckBox ID="CheckBoxSelect" runat="server" />
</td>
<td width="100">
<asp:Literal ID="UserID" Text='<%#Eval("UserID")%>' Visible="false" runat="server"></asp:Literal>
<asp:Literal ID="UserName" Text='<%#Eval("UserName")%>' runat="server"></asp:Literal>
</td>
<td width="100">
<asp:TextBox ID="txtTrueName" Width="90px" Text='<%#Eval("TrueName")%>' runat="server"></asp:TextBox>
</td>
<td width="60">
<asp:DropDownList ID="DropDownListSex" runat="server">
<asp:ListItem Text="男" Value="男"></asp:ListItem>
<asp:ListItem Text="女" Value="女"></asp:ListItem>
</asp:DropDownList>
</td>
<td width="120">
<asp:TextBox ID="txtPhone" Width="110px" Text='<%#Eval("Phone")%>' runat="server"></asp:TextBox>
</td>
<td width="120">
<asp:TextBox ID="txtEmail" Width="90px" Text='<%#Eval("Email")%>' runat="server"></asp:TextBox>
</td>
<td width="60">
<asp:DropDownList ID="DropDownListActivity" runat="server">
<asp:ListItem Text="是" Value="true"></asp:ListItem>
<asp:ListItem Text="否" Value="false"></asp:ListItem>
</asp:DropDownList>
</td>
<td width="80">
<asp:LinkButton ID="LinkButtonUpdate" Text="更新" CommandName="Update" runat="server"></asp:LinkButton>
<asp:LinkButton ID="LinkButtonCancel" Text="取消" CommandName="Cancel" runat="server"></asp:LinkButton>
</td>
<td width="60">
<asp:LinkButton ID="LinkButtonDelete" Text="删除" CommandName="Delete" runat="server"
OnClientClick="javascript:return ConfirmDel()"></asp:LinkButton>
</td>
</tr>
</table>
</EditItemTemplate>
</asp:DataList>
</td>
</tr>
<tr>
<td>
<table width="800px">
<tr>
<td>
</td>
<td width="500px" align="right">
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" LastPageText="末页" NextPageText="下一页"
PrevPageText="上一页" FirstPageText="首页" OnPageChanged="AspNetPager1_PageChanged"
ShowPageIndexBox="Always">
</webdiyer:AspNetPager>
</td>
</tr>
</table>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
后台代码:
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Permissions.BLL;
using Permissions.Model;
using Maticsoft.Common;
public partial class Admin_Account_UserManager : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataListBind();
}
}
private void DataListBind()
{
try
{
Permissions.BLL.Accounts_Users Users = new Permissions.BLL.Accounts_Users();
int TotalCount = 0;
List<Permissions.Model.Accounts_Users> UserInfoList = Users.GetModelList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, "", out TotalCount);
AspNetPager1.RecordCount = TotalCount;
this.DataListUserManager.DataSource = UserInfoList;
this.DataListUserManager.DataBind();
}
catch (Exception ex)
{
Maticsoft.Common.MessageBox.ShowAjax(UpdatePanel1, ex.Message);
}
}
//DataList编辑
protected void DataListUserManager_EditCommand(object source, DataListCommandEventArgs e)
{
try
{
DataListUserManager.EditItemIndex = e.Item.ItemIndex;
DataListBind();
DropDownList DropDownListSex = (DropDownList)DataListUserManager.Items[e.Item.ItemIndex].FindControl("DropDownListSex"); //(DropDownList)e.Item.FindControl("DropDownListSex");
DropDownList DropDownListActivity = (DropDownList)DataListUserManager.Items[e.Item.ItemIndex].FindControl("DropDownListActivity"); //(DropDownList)e.Item.FindControl("DropDownListSex");
int UserID = int.Parse(((Literal)DataListUserManager.Items[e.Item.ItemIndex].FindControl("UserID")).Text);
Permissions.Model.Accounts_Users UserInfo = (new Permissions.BLL.Accounts_Users()).GetModel(UserID);
DropDownListSex.Text = UserInfo.Sex;
DropDownListActivity.Text = UserInfo.Activity.ToString().ToLower();
}
catch (Exception ex)
{
Maticsoft.Common.MessageBox.ShowAjax(UpdatePanel1, ex.Message);
}
}
//DataList取消
protected void DataListUserManager_CancelCommand(object source, DataListCommandEventArgs e)
{
DataListUserManager.EditItemIndex = -1;
DataListBind();
}
//DataList更新
protected void DataListUserManager_UpdateCommand(object source, DataListCommandEventArgs e)
{
try
{
int UserID = int.Parse(((Literal)e.Item.FindControl("UserID")).Text.Trim());
Permissions.BLL.Accounts_Users Users = new Permissions.BLL.Accounts_Users();
Permissions.Model.Accounts_Users UserInfo = Users.GetModel(UserID);
UserInfo.TrueName = ((TextBox)e.Item.FindControl("txtTrueName")).Text.Trim();
UserInfo.Sex = ((DropDownList)e.Item.FindControl("DropDownListSex")).Text.Trim();
UserInfo.Phone = ((TextBox)e.Item.FindControl("txtPhone")).Text.Trim();
UserInfo.Email = ((TextBox)e.Item.FindControl("txtEmail")).Text.Trim();
UserInfo.Activity = bool.Parse(((DropDownList)e.Item.FindControl("DropDownListActivity")).Text.Trim());
Users.Update(UserInfo);
DataListUserManager.EditItemIndex = -1;
DataListBind();
}
catch (Exception ex)
{
Maticsoft.Common.MessageBox.ShowAjax(UpdatePanel1, ex.Message);
}
}
//DataList删除
protected void DataListUserManager_DeleteCommand(object source, DataListCommandEventArgs e)
{
try
{
int UserID = int.Parse(((Literal)e.Item.FindControl("UserID")).Text.Trim());
Permissions.BLL.Accounts_Users Users = new Permissions.BLL.Accounts_Users();
Users.Delete(UserID);
Maticsoft.Common.MessageBox.ShowAjax(this.UpdatePanel1, "删除成功!");
DataListBind();
}
catch (Exception ex)
{
Maticsoft.Common.MessageBox.ShowAjax(UpdatePanel1, ex.Message);
}
}
//分页
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
DataListBind();
}
//全选
protected void CheckBoxSelectAll_CheckedChanged(object sender, EventArgs e)
{
try
{
foreach (DataListItem item in DataListUserManager.Items)
{
CheckBox chkbox = item.FindControl("CheckBoxSelect") as CheckBox;
if (this.CheckBoxSelectAll.Checked)
{
chkbox.Checked = true;
}
else
{
chkbox.Checked = false;
}
}
}
catch (Exception ex)
{
Maticsoft.Common.MessageBox.ShowAjax(UpdatePanel1, ex.Message);
}
}
//删除选中的
protected void ButtonDel_Click(object sender, EventArgs e)
{
try
{
if (HasSelect() == false)//判断是否选择了要删除的项
{
Maticsoft.Common.MessageBox.ShowAjax(UpdatePanel1, "请选择要删除的项");
return;
}
StringBuilder SBUserIDList = new StringBuilder();
foreach (DataListItem item in DataListUserManager.Items)
{
CheckBox chkbox = item.FindControl("CheckBoxSelect") as CheckBox;
if (chkbox.Checked)
{
Literal literal = item.FindControl("UserID") as Literal;
if (!string.IsNullOrEmpty(literal.Text))
{
SBUserIDList.Append(literal.Text + ",");
}
}
}
string UserIDList = Maticsoft.Common.StringPlus.DelLastChar(SBUserIDList.ToString(), ",");
Permissions.BLL.Accounts_Users Users = new Permissions.BLL.Accounts_Users();
if (Users.DeleteList(UserIDList))
{
Maticsoft.Common.MessageBox.ShowAjax(UpdatePanel1, "删除成功!");
}
DataListBind();
}
catch (Exception ex)
{
Maticsoft.Common.MessageBox.ShowAjax(UpdatePanel1, ex.Message);
}
}
private bool HasSelect()//判断是否选择了要删除的项
{
bool hasChecked = false;
foreach (DataListItem item in DataListUserManager.Items)
{
CheckBox chkbox = item.FindControl("CheckBoxSelect") as CheckBox;
if (chkbox.Checked)
{
hasChecked = true;
break;
}
}
return hasChecked;
}
}