zoukankan      html  css  js  c++  java
  • ASP.NET中GridView批量删除

    前台:
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%" DataKeyNames="news_id" Css AllowPaging="True" PageSize="5" > <Columns> <asp:TemplateField> <ItemTemplate> &nbsp;<asp:CheckBox ID="cbSelect" runat="server" /> </ItemTemplate> <ItemStyle HorizontalAlign="Center" Width="30px" /> </asp:TemplateField> <asp:BoundField DataField="news_title" HeaderText="最新动态标题"> <ItemStyle HorizontalAlign="Center" Width="200px" /> </asp:BoundField> <asp:BoundField DataField="news_body" HeaderText="最新动态内容" > <ItemStyle HorizontalAlign="Center" /> </asp:BoundField> <asp:BoundField DataField="news_id" Visible="False" /> </Columns> <PagerStyle HorizontalAlign="Center" /> </asp:GridView> </td> </tr> <tr> <td valign="top"> <asp:CheckBox ID="cbAll" runat="server" Text="全选" AutoPostBack="True" OnCheckedChanged="CheckAll" Css /> <asp:Button ID="btnDelete" runat="server" Text="删除所选" OnClick="btnDelete_Click" Css /></td> </tr>

    后台:

    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.SqlClient;
    public partial class admin_admin_news : System.Web.UI.Page
    {
         protected void Page_Load(object sender, EventArgs e)
         {
             if (!Page.IsPostBack)
             {
                 btnDelete.Attributes.Add("onclick", "return confirm('您真的要删除该行数据吗?');");
                 BindView();
             }
         }
         protected void CheckAll(object sender, EventArgs e)
         {
             CheckBox cbAll = (CheckBox)sender;
             if (cbAll.Text == "全选")
             {
                 foreach (System.Web.UI.WebControls.GridViewRow gr in this.GridView1.Rows)
                 {
                     CheckBox chk = (CheckBox)gr.FindControl("cbSelect");
                     chk.Checked = cbAll.Checked;
                 }
             }
         }
         public void BindView()
         {
             SqlConnection con = Data.BuilderCon();
             String Sel = "select * from news order by news_id desc";
             DataSet ds = Data.BuilderSet(Sel);
             this.GridView1.DataSource = ds;
             this.GridView1.DataBind();
         }
         protected void btnDelete_Click(object sender, EventArgs e)
         {
             foreach (GridViewRow dgi in this.GridView1.Rows)
             {
                 CheckBox cb = (CheckBox)dgi.FindControl("cbSelect");
                 if (cb.Checked)
                 {
                     Int32 index = dgi.RowIndex;
                     DataKey key = this.GridView1.DataKeys[index];
                     String newsid = key.Values["news_id"].ToString();
                     //以下执行删除操作
                     String strSql = "delete news where news_id = '" + newsid + "'";
                     SqlCommand com = Data.BuilderCom(strSql);
                     Int32 num = com.ExecuteNonQuery();
                     if (num > 0)
                     {
                         Response.Write("<script>alert('成功删除所选!');</script>");
                     }
                     cbAll.Checked = false;
                 }
             }
             this.GridView1.PageIndex = 0;
             BindView();
         }
    }
  • 相关阅读:
    ubuntu-虚拟机跟主机资源共享的实现方法
    git- 仓库创建、修改、提交、撤销
    theme- 工作原理
    makeMtk- user 版本编译
    Button- 自定义控件添加自定义属性
    actionMode
    screen-Orientation 横竖屏设置
    worktools-mmx 添加编译模块
    eclipse- MAT安装及使用
    worktools-monkey 测试工具的使用
  • 原文地址:https://www.cnblogs.com/it-bobo/p/3860770.html
Copyright © 2011-2022 走看看