gridview含多页的批量删
book_admin.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="book_admin.aspx.cs" Inherits="admin_book_admin" %>
留言管理
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();
}
}
本文作者:佚名 来源:本站原创
CIO之家 www.ciozj.com 微信公众号:imciow