网络编程 
首页 > 网络编程 > 浏览文章

ASP.NET Gridview 中使用checkbox删除的2种方法实例分享

(编辑:jimmy 日期: 2024/5/9 浏览:3 次 )
方法一:
后台代码:
复制代码 代码如下:
 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>
上一篇:MVC后台创建Json(List)前台接受并循环读取实例
下一篇:asp.net4.0框架下验证机制失效的原因及处理办法
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网