程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> ASP.NET Gridview 中使用checkbox刪除的2種方法實例分享

ASP.NET Gridview 中使用checkbox刪除的2種方法實例分享

編輯:ASP.NET基礎
方法一:
後台代碼:
復制代碼 代碼如下:
 protected void btn_delete_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <this.GridView1.Rows.Count; i++)
        {
            int id = Convert.ToInt32(this.GridView1.DataKeys[i].Value);
            if ((this.GridView1.Rows[i].Cells[0].FindControl("CheckBox1") as CheckBox).Checked == true)
            {
                Delete(id);
                ClientScript.RegisterStartupScript(GetType(),"提示","<script>alert('刪除成功!')</script>");
            }
        }
        this.GridView1.DataBind();
    }//刪除
    private void Delete(int id)
    {
        using (SqlConnection conn = new SqlConnection(str))
        {
            conn.Open();
            SqlCommand comm = conn.CreateCommand();
            comm.CommandText = "delete from Notice_Msg where id=@id";
            comm.Parameters.Add(new SqlParameter("@id", id));
            comm.ExecuteNonQuery();
        }
    }

前台代碼:
復制代碼 代碼如下:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="id">

另外還得添加一列,讓其綁定的字段為id,並且把這一列的visable屬性設為false
方法二:
後台:
復制代碼 代碼如下:
 protected void btn_delete_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in this.GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox ckb = row.Cells[2].FindControl("CheckBox1") as CheckBox;
                if (ckb.Checked)
                {
                    using (SqlConnection sqlCnn = new SqlConnection(str))
                    {
                        using (SqlCommand sqlCmm = sqlCnn.CreateCommand())
                        {
                            sqlCmm.CommandText = "delete from Regime_Table where id='" + row.Cells[0].Text + "' ";
                            sqlCnn.Open();
                            int a= sqlCmm.ExecuteNonQuery();
                            if (a>0)
                            {
                                ClientScript.RegisterStartupScript(GetType(),"提示","<script>alert('刪除成功!')</script>");
                            }
                            else
                            {
                                ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('刪除失敗!')</script>");
                            }
                            this.DataBind();
                        }
                    }
                }
            }
        }
    }

前台:
復制代碼 代碼如下:
<style type="text/css">
    .Hidden
    {
        display:none;
    }
    </style>
<asp:BoundField DataField="id" HeaderText="編號" >
                   <HeaderStyle CssClass="Hidden" />
                   <ItemStyle CssClass="Hidden" />
                   </asp:BoundField>

新增加一列,這一列綁定id字段,並且visable屬性不能為false,否則取不出值來。

checkbox全選功能:
復制代碼 代碼如下:
<script type="text/jscript">
         function change(sender) {
             var table = document.getElementById("GridView1");
             for (var i = 1; i < table.rows.length; i++) {
                 table.rows[i].cells[1].getElementsByTagName("input")[0].checked = sender.checked;
             }
         }
    </script>
<HeaderTemplate>
      <input id="Checkbox2" type="checkbox" onclick="change(this)"/>
             全選
         </HeaderTemplate>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved