前台:
<%@ PageLanguage="C#"AutoEventWireup="true"CodeBehind="UserManage.aspx.cs"Inherits="SPDMWebApp.UserManage"%>
<form id="Form1" method="post" runat="server" style="width: 570px">
<%--<center>--%>
<asp:TextBox runat="server" ID="TextBoxSearch"></asp:TextBox>
<asp:Button runat="server" ID="Search" Text="搜索用户" onclick="Search_Click"></asp:Button>
<asp:Label runat="server" Width="40px"></asp:Label>
<asp:checkbox runat="server" Text="显示系统管理用户"></asp:checkbox>
<div style="height: 20px"></div>
<div>
<asp:button runat="server" text="创建用户" Width="80px" onclick="Unnamed5_Click" />
<asp:Label runat="server" Width="40px"></asp:Label>
<asp:button runat="server" text="删除用户" Width="80px" ID="BatchDelete" onclick="Unnamed7_Click" />
<asp:Label runat="server" Width="40px"></asp:Label>
<asp:button runat="server" text="与LDAP同步" Width="80px" />
<asp:Label runat="server" Width="40px"></asp:Label>
<asp:button runat="server" text="批量导入用" Width="80px" />
</div>
<br />
<asp:panel runat="server" BorderStyle="Groove" Width="560px" >
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Font-Size="X-Small"
CellPadding="4" ForeColor="#333333" GridLines="Vertical" Width="550px"
AllowPaging="True" onpageindexchanging="GridView1_PageIndexChanging"
onrowdatabound="GridView1_RowDataBound" onrowdeleting="GridView1_RowDeleting"
PageSize="5" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>选择
<%--<asp:Buttonrunat="server" Text="全选"onclick="Edit"></asp:Button>--%>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="用户名">
<ItemTemplate>
<asp:Literal ID="Literal1" runat="server" Text= '<%# Eval("UserName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="用户全名">
<ItemTemplate>
<asp:Literal ID="Literal2" runat="server" Text='<%# Eval("UserChineseName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="部门">
<ItemTemplate>
<asp:Literal ID="Literal3" runat="server" Text='<%# Eval("DepartmentID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="电子邮件">
<ItemTemplate>
<asp:Literal ID="Literal4" runat="server" Text='<%# Eval("Email") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="编辑">
<ItemTemplate>
<asp:LinkButton ID="edit" runat="server" onclick="Edit" CommandArgument='<%# Eval("UID") %>'>编辑</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField DeleteText="<div id="de"onclick="JavaScript:return confirm('确定删除吗?')">删除</div> "
HeaderText="删除" ShowDeleteButton="True" >
<ItemStyle ForeColor="#0066FF" />
</asp:CommandField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
<br />
<asp:Button runat="server" Text="全选" ID="Button_SelectAll"
onclick="Button_SelectAll_Click" ></asp:Button>
<asp:Label runat="server" Width="10px"></asp:Label>
<asp:Button runat="server" Text="全不选" ID="Button_NoSelectAll"
onclick="Button_NoSelectAll_Click"></asp:Button>
</asp:panel>
<%--</center>--%>
</form>
后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using BLL;
namespace SPDMWebApp
{
public partial class UserManage : System.Web.UI.Page
{
public UserMap map = new UserMap();
protectedvoid Page_Load(objectsender, EventArgs e)
{
if(!IsPostBack)
{
bind();
}
}
protectedvoid Unnamed5_Click(objectsender, EventArgs e)
{
Response.Redirect("UserEdit.aspx?id=-1");
}
protectedvoid GridView1_PageIndexChanging(object sender, GridViewPageEventArgse)
{
try
{
RememberOldValues();
GridView1.PageIndex =e.NewPageIndex;
bind();
RePopulateValues();
}
catch
{ }
}
protectedvoid GridView1_RowDeleting(object sender, GridViewDeleteEventArgse)
{
try
{
stringuserID = GridView1.DataKeys[e.RowIndex].Value.ToString();
//*****************************LDAP模式下,用户无法在PDM 系统中删除**************************
//如何判断?????
boolisSuccessful = map.Delete(userID);
if(isSuccessful)
Alert("删除成功!");
else
Alert("删除失败!");
}
catch(Exception ex)
{
stringerror = ex.ToString();
Alert("删除失败!");
}
if(0 == TextBoxSearch.Text.Trim().Length)
bind();
else
bindBySearch();
}
protectedvoid GridView1_RowDataBound(object sender, GridViewRowEventArgse)
{
inti;
for(i = 0; i < GridView1.Rows.Count + 1; i++) // 网上代码这里没有加1,发现当移入移出第一行时颜色未变。
{
if(e.Row.RowType == DataControlRowType.DataRow) //判断当前行是否是数据行
{
//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FD2F00'");
//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
}
protectedvoid Edit(objectsender, EventArgs e)
{
try
{
LinkButtonlb = (LinkButton)sender;
stringid = lb.CommandArgument;
Response.Redirect("UserEdit.aspx?id=" + id);
}
catch {}
}
protectedvoid Button_SelectAll_Click(object sender, EventArgse)
{
try
{
foreach(GridViewRow row inGridView1.Rows)
{
CheckBoxmyCheckBox = (CheckBox)row.FindControl("chkSelect");
myCheckBox.Checked = true;
}
}
catch{ }
}
protectedvoid Button_NoSelectAll_Click(object sender, EventArgse)
{
try
{
foreach(GridViewRow row inGridView1.Rows)
{
CheckBoxmyCheckBox = (CheckBox)row.FindControl("chkSelect");
myCheckBox.Checked = false;
}
}
catch{ }
}
// 删除Checkbox选中的数据
protectedvoid Unnamed7_Click(objectsender, EventArgs e)
{
try
{
List<string> idList = newList<string>();
foreach(GridViewRow row inGridView1.Rows)
{
CheckBoxmyCheckBox = (CheckBox)row.FindControl("chkSelect");
if(myCheckBox.Checked)
{
string id = GridView1.DataKeys[row.RowIndex].Value.ToString();
if (!idList.Contains(id))
idList.Add(id);
}
}
if(0 == idList.Count)
{
Alert("请选择要删除的用户");
return;
}
BatchDelete.Attributes.Add("onclick", "returnconfirm('确认要删除该用户吗?')");
boolisSuccessful = map.Delete(idList);
if(isSuccessful)
Alert("删除成功!");
else
Alert("删除失败!");
}
catch(Exception ex)
{
stringerror = ex.ToString();
Alert("删除失败!");
}
bind();
}
//搜索用户
protectedvoid Search_Click(objectsender, EventArgs e)
{
DataSetmyds = new DataSet();
try
{
stringword = TextBoxSearch.Text.Trim();
if(0 == word.Length)
{
Alert("请输入要搜索的用户!");
bind();
return;
}
myds = map.Search(word);
if(0 == myds.Tables[0].Rows.Count)
{
//this.Button_SelectAll.Visible= false;
//this.Button_NoSelectAll.Visible= false;
Alert("没有找到相关用户!");
bind();
return;
}
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "UID" };//设定主键默认不显示该列
GridView1.DataBind();
}
catch(Exception ex)
{
stringerror = ex.ToString();
Alert("没有找到相关用户!");
bind();
}
}
//绑定
public void bind()
{
TextBoxSearch.Text = "";
DataSetmyds = new DataSet();
try
{
myds = map.GetDataSet();
if(0 == myds.Tables[0].Rows.Count)
{
this.Button_SelectAll.Visible= false;
this.Button_NoSelectAll.Visible= false;
}
GridView1.DataSource =myds;
GridView1.DataKeyNames = new string[] { "UID" };//设定主键默认不显示该列
GridView1.DataBind();
}
catch(Exception ex)
{
stringerror = ex.ToString();
}
}
//绑定搜索词
public void bindBySearch()
{
DataSetmyds = new DataSet();
try
{
stringword = TextBoxSearch.Text.Trim();
if(0 == word.Length)
{
bind();
return;
}
myds = map.Search(word);
if(0 == myds.Tables[0].Rows.Count)
{
//this.Button_SelectAll.Visible= false;
//this.Button_NoSelectAll.Visible= false;
bind();
return;
}
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "UID" };//设定主键默认不显示该列
GridView1.DataBind();
}
catch(Exception ex)
{
stringerror = ex.ToString();
bind();
}
}
#region 分页时Checkbox的状态处理
//记录checkbox的状态
privatevoid RememberOldValues()
{
ArrayListcategoryIDList = new ArrayList();
intindex = -1;
foreach(GridViewRow row inGridView1.Rows)
{
index = (int)GridView1.DataKeys[row.RowIndex].Value;
boolresult = ((CheckBox)row.FindControl("chkSelect")).Checked;
//Check in the Session
if(Session["CHECKED_ITEMS"] != null)
categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if(result)
{
if(!categoryIDList.Contains(index))
categoryIDList.Add(index);
}
else
categoryIDList.Remove(index);
}
if(categoryIDList != null &&categoryIDList.Count > 0)
Session["CHECKED_ITEMS"] = categoryIDList;
}
//从记录的状态恢复Checkbox的状态
privatevoid RePopulateValues()
{
ArrayListcategoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if(categoryIDList != null &&categoryIDList.Count > 0)
{
foreach(GridViewRow row inGridView1.Rows)
{
intindex = (int)GridView1.DataKeys[row.RowIndex].Value;
if(categoryIDList.Contains(index))
{
CheckBox myCheckBox = (CheckBox)row.FindControl("chkSelect");
myCheckBox.Checked = true;
}
}
}
}
#endregion
///<summary>
///弹出对话框
///</summary>
///<paramname="message">提示语</param>
public void Alert(stringmessage)
{
stringnewMessage = String.Format("<script language=javascript>alert('{0}');</script>", message);
Response.Write(newMessage);
}
}
}
联系客服