欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net gridview多页时的批量删除

程序员文章站 2024-02-05 12:21:04
book_admin.aspx复制代码 代码如下:
book_admin.aspx
复制代码 代码如下:

<asp:gridview id="grwbook" runat="server" cellpadding="4" forecolor="#333333" gridlines="none" autogeneratecolumns="false" allowpaging="true" datakeynames="id" onpageindexchanging="grwbook_pageindexchanging" pagesize="12">
    <footerstyle backcolor="#507cd1" font-bold="true" forecolor="white" />
    <rowstyle backcolor="#eff3fb" />
    <editrowstyle backcolor="#2461bf" />
    <selectedrowstyle backcolor="#d1ddf1" font-bold="true" forecolor="#333333" />
    <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" />
    <asp:boundfield datafield="username" headertext="姓名">
    <itemstyle width="60px" />
    </asp:boundfield>
    <asp:boundfield datafield="comments" headertext="评论">
    <itemstyle width="500px"  />
    </asp:boundfield>
    <asp:boundfield datafield="postdate" headertext="日期" />
    <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>
    </columns>
    <pagersettings mode="nextpreviousfirstlast" firstpagetext="第一页" lastpagetext="末页" nextpagetext="下一页" previouspagetext="上一页" />
</asp:gridview>

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,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();

    }

    //执行删除操作
    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();
                //response.redirect("book_admin.aspx");
            }
            catch (exception ex)
            {
                //若有错误发生,输出错误信息
                common.salert(ex.message);
            }
            finally
            {

            }
        }
        else
        {
            common.salert("您还没有选中有删除的项");
        }
    }


}