界面参考:
aspx页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %>
<!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">
function GetAllCheckBox(CheckAll){
//var items=document.getElementsByTagName("input");
var items=document.getElementsByName("CheckBoxGroup");
for(var i=0;i<items.length;i++){
if(items[i].type=="checkbox"){
//全选的CheckBox.checked为true则为为全选,false则反选
items[i].checked=CheckAll.checked;
}
}
}
//检查是否至少选择了一项
function CheckHasSelectedItem()
{
//得到所有name为CheckBoxGroup
var checkbox = document.all.CheckBoxGroup;
if(checkbox==null)
{
return false;
}
if(checkbox.length+""!="undefined")
{
for( var i=0;i<checkbox.length;i++ )
{
if(checkbox[i].checked)
{
return true;
}
}
}
/*当数据库中只有一条记录时,
* document.all.CheckBoxGroup取出的是Object对象而不是数组
所以我们在这还要判断得到Object对象时是否被选中
(可以在数据库中只去一条数据测试(top 1),查看是否有这个小Bug)
*/
else if(checkbox.checked)
{
return true;
}
else
{
return false;
}
}
function ConfirmDelete()
{
if(CheckHasSelectedItem())//如果至少选择了一项
{
return confirm("确认删除选中的用户?");
}
else
{
alert("请至少选择一项!");
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging"
EmptyDataText="无数据" onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
全选<input type="checkbox" οnclick="javascript:GetAllCheckBox(this);" name="selAll" />
</HeaderTemplate>
<ItemTemplate>
<input type="checkbox" name="CheckBoxGroup" value='<%#Eval("userid") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UserName" HeaderText="用户名" />
<asp:HyperLinkField DataNavigateUrlFields="userId" DataNavigateUrlFormatString="ShowUser.aspx?UserId={0}"
DataTextField="RealName" HeaderText="查看" />
<asp:BoundField DataField="Age" HeaderText="年龄" />
<asp:CheckBoxField DataField="Sex" HeaderText="男" />
<asp:BoundField DataField="Phone" HeaderText="电话" />
<asp:ButtonField DataTextField="Mobile" HeaderText="手机" Text="按钮" />
<asp:TemplateField>
<HeaderTemplate>
邮件啦</HeaderTemplate>
<AlternatingItemTemplate>
<a href='emailto:<%#Eval("Email") %>'>
<%#Eval("Email") %></a>
</AlternatingItemTemplate>
<ItemTemplate>
<%#Eval("Email") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="删除"
OnClientClick="return ConfirmDelete() " οnclick="Button1_Click" />
</div>
</form>
</body>
</html>
aspx.cs
using System; using System.Collections; using System.Configuration; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Data.SqlClient; public partial class Default7 : System.Web.UI.Page { SqlConnection conn; protected void Page_Load(object sender, EventArgs e) { conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sql2005"].ConnectionString); if (!IsPostBack) { GVBinds(0); } } /// <summary> /// 绑定数据 /// </summary> /// <param name="pageIndex"></param> private void GVBinds(int pageIndex) { DataTable dt = new DataTable(); conn.Open(); SqlCommand comm = new SqlCommand("select * from userInfo", conn); SqlDataReader sdr = comm.ExecuteReader(); dt.Load(sdr); GridView1.AutoGenerateColumns = false; GridView1.AllowPaging = true; GridView1.PageSize = 5; GridView1.PageIndex = pageIndex; GridView1.DataSource = dt.DefaultView; GridView1.DataBind(); conn.Close(); sdr.Close(); } /// <summary> /// 当页索引发生改变时 /// </summary> protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GVBinds(e.NewPageIndex); } /// <summary> /// 删除事件 /// </summary> protected void Button1_Click(object sender, EventArgs e) { conn.Open(); SqlCommand comm = new SqlCommand("delete from userInfo where userid in ("+Request["CheckBoxGroup"]+")",conn); if (comm.ExecuteNonQuery()>0) { Page.ClientScript.RegisterStartupScript(this.GetType(),"success","alert('删除成功');window.location='Default7.aspx'",true); } else { Page.ClientScript.RegisterStartupScript(this.GetType(),"fail","alert('删除失败');",true); } conn.Close(); } /// <summary> /// 光棒效果 /// </summary> protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType==DataControlRowType.DataRow) { //注意这里的this.style.backgroundColor='#6699ff'中的'#6699ff'一定要加''号,因为输出的是脚本,加上''代表字符串. e.Row.Attributes.Add("onmouseover","currentColor=this.style.backgroundColor;this.style.backgroundColor='#6699ff'"); e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentColor;"); //给某个单元格也加上光棒效果,比如在博客的文章列表标题添加光帮效果. e.Row.Cells[2].Attributes.Add("onmouseover", "currentColor=this.style.backgroundColor;this.style.backgroundColor='black';"); e.Row.Cells[2].Attributes.Add("onmouseout", "this.style.backgroundColor=currentColor"); } } }