前臺(tái):
<%@ 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="搜索用戶(hù)" onclick="Search_Click"></asp:Button>
<asp:Label runat="server" Width="40px"></asp:Label>
<asp:checkbox runat="server" Text="顯示系統(tǒng)管理用戶(hù)"></asp:checkbox>
<div style="height: 20px"></div>
<div>
<asp:button runat="server" text="創(chuàng)建用戶(hù)" Width="80px" onclick="Unnamed5_Click" />
<asp:Label runat="server" Width="40px"></asp:Label>
<asp:button runat="server" text="刪除用戶(hù)" 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="批量導(dǎo)入用" 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="用戶(hù)名">
<ItemTemplate>
<asp:Literal ID="Literal1" runat="server" Text= '<%# Eval("UserName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="用戶(hù)全名">
<ItemTemplate>
<asp:Literal ID="Literal2" runat="server" Text='<%# Eval("UserChineseName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="部門(mén)">
<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>
后臺(tái):
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模式下,用戶(hù)無(wú)法在PDM 系統(tǒng)中刪除**************************
//如何判斷?????
boolisSuccessful = map.Delete(userID);
if(isSuccessful)
Alert("刪除成功!");
else
Alert("刪除失?。?/span>");
}
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++) // 網(wǎng)上代碼這里沒(méi)有加1,發(fā)現(xiàn)當(dāng)移入移出第一行時(shí)顏色未變。
{
if(e.Row.RowType == DataControlRowType.DataRow) //判斷當(dāng)前行是否是數(shù)據(jù)行
{
//當(dāng)鼠標(biāo)停留時(shí)更改背景色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FD2F00'");
//當(dāng)鼠標(biāo)移開(kāi)時(shí)還原背景色
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選中的數(shù)據(jù)
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("請(qǐng)選擇要?jiǎng)h除的用戶(hù)");
return;
}
BatchDelete.Attributes.Add("onclick", "returnconfirm('確認(rèn)要?jiǎng)h除該用戶(hù)嗎?')");
boolisSuccessful = map.Delete(idList);
if(isSuccessful)
Alert("刪除成功!");
else
Alert("刪除失?。?/span>");
}
catch(Exception ex)
{
stringerror = ex.ToString();
Alert("刪除失?。?/span>");
}
bind();
}
//搜索用戶(hù)
protectedvoid Search_Click(objectsender, EventArgs e)
{
DataSetmyds = new DataSet();
try
{
stringword = TextBoxSearch.Text.Trim();
if(0 == word.Length)
{
Alert("請(qǐng)輸入要搜索的用戶(hù)!");
bind();
return;
}
myds = map.Search(word);
if(0 == myds.Tables[0].Rows.Count)
{
//this.Button_SelectAll.Visible= false;
//this.Button_NoSelectAll.Visible= false;
Alert("沒(méi)有找到相關(guān)用戶(hù)!");
bind();
return;
}
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "UID" };//設(shè)定主鍵默認(rèn)不顯示該列
GridView1.DataBind();
}
catch(Exception ex)
{
stringerror = ex.ToString();
Alert("沒(méi)有找到相關(guān)用戶(hù)!");
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" };//設(shè)定主鍵默認(rèn)不顯示該列
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" };//設(shè)定主鍵默認(rèn)不顯示該列
GridView1.DataBind();
}
catch(Exception ex)
{
stringerror = ex.ToString();
bind();
}
}
#region 分頁(yè)時(shí)Checkbox的狀態(tài)處理
//記錄checkbox的狀態(tài)
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;
}
//從記錄的狀態(tài)恢復(fù)Checkbox的狀態(tài)
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>
///彈出對(duì)話框
///</summary>
///<paramname="message">提示語(yǔ)</param>
public void Alert(stringmessage)
{
stringnewMessage = String.Format("<script language=javascript>alert('{0}');</script>", message);
Response.Write(newMessage);
}
}
}
聯(lián)系客服