首页  ·  知识 ·  云计算
gridview多页时的批量删除
佚名  本站原创  综合  编辑:dezai  图片来源:网络
gridview含多页的批量删 book_admin.aspx %@ Page Language="C#" AutoEventWireup="true" CodeFile="book_admin.aspx.cs" Inherits

gridview含多页的批量删

book_admin.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="book_admin.aspx.cs" Inherits="admin_book_admin" %>



    留言管理
   
   


   
   


       

       
         
           
                            ErrorMessage="您只能输入数字" ValidationExpression="^[1-9]\d*$" Display="Dynamic">
                            Display="Dynamic" ErrorMessage="必须填写">

      
                                OnRowUpdating="grwBook_RowUpdating" OnRowCancelingEdit="grwBook_RowCancelingEdit" OnRowDataBound="grwBook_RowDataBound">
           
           
           
           
           
           
           
           
           
           
           

           
               
                <%#common.qqReplace(Eval("Comments").ToString()) %>


                管理员回复:<%#Eval("Replay") %>
               

                
                
               

               
           

           
           
           
               
               
               

           

            
           

           
       

       
       
         

   


   


 

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
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读