gridview多页数据批量删除
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="book_admin.aspx.cs" Inherits="admin_book_admin" %>
<!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>
<link rel="Stylesheet" type="text/css" href="/css/admin.css" />
<style type="text/css">
.selectfontcolor{
color:white;
}
.lineheight{
line-height:20px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<div style="text-align:center;padding-bottom:5px;">
<asp:TextBox ID="tbxSearch" runat="server" Text="输入ID搜索" style="color:#999999;" onclick="this.value=''"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" onClick="btnSearch_Click" Text="搜索" />
<asp:Button ID="btnAll" runat="server" onClick="btnAll_Click" Text="全部" />
<asp:RegularExpressionValidator ID="rv1" runat="server" ControlToValidate="tbxSearch"
ErrorMessage="您只能输入数字" ValidationExpression="^[1-9]\d*$" Display="Dynamic"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="tbxSearch"
Display="Dynamic" ErrorMessage="必须填写"></asp:RequiredFieldValidator></div>
<asp:GridView ID="grwBook" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false" AllowPaging="true" DataKeyNames="ID" OnPageIndexChanging="grwBook_PageIndexChanging" PageSize="10" OnRowEditing="grwBook_RowEditing"
OnRowUpdating="grwBook_RowUpdating" OnRowCancelingEdit="grwBook_RowCancelingEdit" OnRowDataBound="grwBook_RowDataBound">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" ForeColor="white" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="ID" HeaderText = "ID" ReadOnly="true" />
<asp:BoundField DataField="UserName" HeaderText="姓名" ReadOnly="true">
<ItemStyle Width="60px" />
</asp:BoundField>
<asp:TemplateField HeaderText="评论" ItemStyle-CssClass="lineheight">
<ItemTemplate>
<%#common.qqReplace(Eval("Comments").ToString()) %><br /><br />
<font style="font-weight:bold;color:#DB3434;padding:0;">管理员回复:</font><font color="#DB3434"><%#Eval("Replay") %></font>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Text='<%#Eval("Replay")%>' runat="server" TextMode="MultiLine" style="width:450px;height:50px;" />
</EditItemTemplate>
<ItemStyle Width="480" />
</asp:TemplateField>
<asp:BoundField DataField="Postdate" HeaderText="日期" ReadOnly="true" />
<asp:HyperLinkField DataNavigateUrlFields="newsid" DataNavigateUrlFormatString="../news_zi.asp?id={0}" HeaderText="文章" DataTextField="newsid" target="_blank" />
<asp:TemplateField HeaderText="操作" >
<ItemTemplate>
<asp:CheckBox runat="Server" ID="cbxId" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="回复" ShowEditButton="True" UpdateText="确定" EditText="回复" CancelText="取消" />
</Columns>
<PagerSettings Mode=NumericFirstLast PageButtonCount="10" FirstPageText="第一页" LastPageText="末页" />
</asp:GridView>
<asp:Button ID="btnDel" runat="server" onClick="btnDel_Click" Text="删除选中" />
<asp:Button ID="btnEnterCheck" runat="server" Text="全选" OnClientClick="checkAll(document.getElementById('form1'),'all')" />
<asp:Button ID="btnCancelAll" runat="server" Text="取消全选" OnClientClick="checkAll(document.getElementById('form1'),'none')" />
</div>
</form>
</body>
</html>
<script language="javascript" type="text/javascript" src="../js/admin.js"></script>
book_admin.aspx.cs
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 System.Data.OleDb;
public partial class admin_book_admin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//判断管理员是否已经登陆
admin.checkadmin();
btnDel.Attributes.Add("onclick", "return confirm('确定删除吗?')");
if (!Page.IsPostBack)
{
datainit();
}
}
protected void grwBook_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.grwBook.PageIndex = e.NewPageIndex;
Session["curretnPage"] = e.NewPageIndex;
datainit();
}
//数据绑定
private void datainit()
{
DataSet ds = db.dataSet("select ID,UserName,Comments,Postdate,Replay,newsid from Feedback order by Postdate desc,newsid desc");
if (ViewState["currentPage"] != null)
{
this.grwBook.PageIndex = Convert.ToInt32(Session["currentPage"]);
}
this.grwBook.DataSource = ds;
this.grwBook.DataBind();
}
//数据绑定,重载
private void datainit(int n)
{
DataSet ds = db.dataSet("select ID,UserName,Comments,Postdate,Replay,newsid from Feedback where ID = "+n+" order by Postdate desc,newsid desc");
if (ViewState["currentPage"] != null)
{
this.grwBook.PageIndex = Convert.ToInt32(Session["currentPage"]);
}
this.grwBook.DataSource = ds;
this.grwBook.DataBind();
}
//执行删除操作
protected void btnDel_Click(object sender, EventArgs e)
{
string sqlText = "(";
for (int i = 0; i < grwBook.Rows.Count; i++)
{
//搜索第n行3列
CheckBox cbx = (CheckBox)grwBook.Rows[i].FindControl("cbxId");
if (cbx.Checked == true)
{
sqlText = sqlText + Convert.ToInt32(grwBook.DataKeys[i].Value) + ",";
}
}
//判断是否有选中
if (sqlText != "(")
{
//去掉最后的逗号,并且加上右括号
sqlText = sqlText.Substring(0, sqlText.Length - 1) + ")";
sqlText = "delete from Feedback where ID in" + sqlText;
try
{
//执行删除语句
db.excuteSql(sqlText);
//重新绑定数据
common.salert("删除成功");
datainit();
}
catch (Exception ex)
{
//若有错误发生,输出错误信息
common.salert(ex.Message);
}
finally
{
grwBook.EditIndex = -1;
}
}
else
{
common.salert("您还没有选中要删除的项");
}
}
//
protected void grwBook_RowEditing(object sender, GridViewEditEventArgs e)
{
grwBook.EditIndex = e.NewEditIndex;
datainit();
}
//更新
protected void grwBook_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string sqlStr = "update feedback set replay = '"
+ common.delSingle(((TextBox)(grwBook.Rows[e.RowIndex].Cells[2].Controls[1])).Text.ToString().Trim()) + "'"
+ " where ID = " + grwBook.DataKeys[e.RowIndex].Value.ToString();
db.excuteSql(sqlStr);
grwBook.EditIndex = -1;
datainit();
}
//取消
protected void grwBook_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grwBook.EditIndex = -1;
datainit();
}
//按下搜索
protected void btnSearch_Click(object sender, EventArgs e)
{
datainit(Convert.ToInt32(this.tbxSearch.Text.Trim()));
}
//鼠标放上去变色
protected void grwBook_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < grwBook.Rows.Count; i++)
{
//首先判断是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//当鼠标停留时改变背景色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.backgroundColor='red'");
//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
}
//按下全部
protected void btnAll_Click(object sender, EventArgs e)
{
datainit();
}
}
<!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>
<link rel="Stylesheet" type="text/css" href="/css/admin.css" />
<style type="text/css">
.selectfontcolor{
color:white;
}
.lineheight{
line-height:20px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<div style="text-align:center;padding-bottom:5px;">
<asp:TextBox ID="tbxSearch" runat="server" Text="输入ID搜索" style="color:#999999;" onclick="this.value=''"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" onClick="btnSearch_Click" Text="搜索" />
<asp:Button ID="btnAll" runat="server" onClick="btnAll_Click" Text="全部" />
<asp:RegularExpressionValidator ID="rv1" runat="server" ControlToValidate="tbxSearch"
ErrorMessage="您只能输入数字" ValidationExpression="^[1-9]\d*$" Display="Dynamic"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="tbxSearch"
Display="Dynamic" ErrorMessage="必须填写"></asp:RequiredFieldValidator></div>
<asp:GridView ID="grwBook" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false" AllowPaging="true" DataKeyNames="ID" OnPageIndexChanging="grwBook_PageIndexChanging" PageSize="10" OnRowEditing="grwBook_RowEditing"
OnRowUpdating="grwBook_RowUpdating" OnRowCancelingEdit="grwBook_RowCancelingEdit" OnRowDataBound="grwBook_RowDataBound">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" ForeColor="white" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="ID" HeaderText = "ID" ReadOnly="true" />
<asp:BoundField DataField="UserName" HeaderText="姓名" ReadOnly="true">
<ItemStyle Width="60px" />
</asp:BoundField>
<asp:TemplateField HeaderText="评论" ItemStyle-CssClass="lineheight">
<ItemTemplate>
<%#common.qqReplace(Eval("Comments").ToString()) %><br /><br />
<font style="font-weight:bold;color:#DB3434;padding:0;">管理员回复:</font><font color="#DB3434"><%#Eval("Replay") %></font>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Text='<%#Eval("Replay")%>' runat="server" TextMode="MultiLine" style="width:450px;height:50px;" />
</EditItemTemplate>
<ItemStyle Width="480" />
</asp:TemplateField>
<asp:BoundField DataField="Postdate" HeaderText="日期" ReadOnly="true" />
<asp:HyperLinkField DataNavigateUrlFields="newsid" DataNavigateUrlFormatString="../news_zi.asp?id={0}" HeaderText="文章" DataTextField="newsid" target="_blank" />
<asp:TemplateField HeaderText="操作" >
<ItemTemplate>
<asp:CheckBox runat="Server" ID="cbxId" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="回复" ShowEditButton="True" UpdateText="确定" EditText="回复" CancelText="取消" />
</Columns>
<PagerSettings Mode=NumericFirstLast PageButtonCount="10" FirstPageText="第一页" LastPageText="末页" />
</asp:GridView>
<asp:Button ID="btnDel" runat="server" onClick="btnDel_Click" Text="删除选中" />
<asp:Button ID="btnEnterCheck" runat="server" Text="全选" OnClientClick="checkAll(document.getElementById('form1'),'all')" />
<asp:Button ID="btnCancelAll" runat="server" Text="取消全选" OnClientClick="checkAll(document.getElementById('form1'),'none')" />
</div>
</form>
</body>
</html>
<script language="javascript" type="text/javascript" src="../js/admin.js"></script>
book_admin.aspx.cs
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 System.Data.OleDb;
public partial class admin_book_admin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//判断管理员是否已经登陆
admin.checkadmin();
btnDel.Attributes.Add("onclick", "return confirm('确定删除吗?')");
if (!Page.IsPostBack)
{
datainit();
}
}
protected void grwBook_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.grwBook.PageIndex = e.NewPageIndex;
Session["curretnPage"] = e.NewPageIndex;
datainit();
}
//数据绑定
private void datainit()
{
DataSet ds = db.dataSet("select ID,UserName,Comments,Postdate,Replay,newsid from Feedback order by Postdate desc,newsid desc");
if (ViewState["currentPage"] != null)
{
this.grwBook.PageIndex = Convert.ToInt32(Session["currentPage"]);
}
this.grwBook.DataSource = ds;
this.grwBook.DataBind();
}
//数据绑定,重载
private void datainit(int n)
{
DataSet ds = db.dataSet("select ID,UserName,Comments,Postdate,Replay,newsid from Feedback where ID = "+n+" order by Postdate desc,newsid desc");
if (ViewState["currentPage"] != null)
{
this.grwBook.PageIndex = Convert.ToInt32(Session["currentPage"]);
}
this.grwBook.DataSource = ds;
this.grwBook.DataBind();
}
//执行删除操作
protected void btnDel_Click(object sender, EventArgs e)
{
string sqlText = "(";
for (int i = 0; i < grwBook.Rows.Count; i++)
{
//搜索第n行3列
CheckBox cbx = (CheckBox)grwBook.Rows[i].FindControl("cbxId");
if (cbx.Checked == true)
{
sqlText = sqlText + Convert.ToInt32(grwBook.DataKeys[i].Value) + ",";
}
}
//判断是否有选中
if (sqlText != "(")
{
//去掉最后的逗号,并且加上右括号
sqlText = sqlText.Substring(0, sqlText.Length - 1) + ")";
sqlText = "delete from Feedback where ID in" + sqlText;
try
{
//执行删除语句
db.excuteSql(sqlText);
//重新绑定数据
common.salert("删除成功");
datainit();
}
catch (Exception ex)
{
//若有错误发生,输出错误信息
common.salert(ex.Message);
}
finally
{
grwBook.EditIndex = -1;
}
}
else
{
common.salert("您还没有选中要删除的项");
}
}
//
protected void grwBook_RowEditing(object sender, GridViewEditEventArgs e)
{
grwBook.EditIndex = e.NewEditIndex;
datainit();
}
//更新
protected void grwBook_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string sqlStr = "update feedback set replay = '"
+ common.delSingle(((TextBox)(grwBook.Rows[e.RowIndex].Cells[2].Controls[1])).Text.ToString().Trim()) + "'"
+ " where ID = " + grwBook.DataKeys[e.RowIndex].Value.ToString();
db.excuteSql(sqlStr);
grwBook.EditIndex = -1;
datainit();
}
//取消
protected void grwBook_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grwBook.EditIndex = -1;
datainit();
}
//按下搜索
protected void btnSearch_Click(object sender, EventArgs e)
{
datainit(Convert.ToInt32(this.tbxSearch.Text.Trim()));
}
//鼠标放上去变色
protected void grwBook_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < grwBook.Rows.Count; i++)
{
//首先判断是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//当鼠标停留时改变背景色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.backgroundColor='red'");
//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
}
//按下全部
protected void btnAll_Click(object sender, EventArgs e)
{
datainit();
}
}