ASP.NET Gridview 中使用checkbox删除的2种方法实例分享
2013-06-09 00:00
776 查看
方法一:
后台代码:
前台代码:
另外还得添加一列,让其绑定的字段为id,并且把这一列的visable属性设为false
方法二:
后台:
前台:
新增加一列,这一列绑定id字段,并且visable属性不能为false,否则取不出值来。
checkbox全选功能:
后台代码:
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>
相关文章推荐
- ASP.NET Gridview 中使用checkbox删除的2种方法实例分享
- ASP.NET Gridview 中使用checkbox删除的方法(两种)
- asp.net checkbox 动态绑定id GridView删除提示
- Asp.Net[C#] 使用GridView 分页,编辑,删除,导出XML完整页面
- 灵活掌握Asp.net MVC中GridView的使用方法
- 使用JavaScript脚本无法直接改变Asp.net中Checkbox控件的Enable属性的解决方法
- 使用ASP.NET中关于代码分离的实例分享
- Asp.Net GridView 在编辑状态中使用动态Dropdownlist方法
- ASP.NET中GridView控件删除数据的两种方法
- asp.net中gridview的checkbox使用总结
- asp.net字符串分割函数使用方法分享
- asp.net下gridview 批量删除的实现方法第1/3页
- asp.net之DataList的使用方法,及分页(存储过程创建),编辑,更新,删除
- asp.net GridView的删除对话框的两种方法
- Jquery插件 Jqgrid在ASP.Net下的使用方法与实例【转】
- ASP.NET中使用javascript獲取GridView,獲取GridView中CheckBox值,並添加記錄
- Asp.net_GridView控件中添加的RadioButton的使用方法
- ASP.NET DEMO 14: 如何在 GridView/DataGrid 模板列中使用自动回发的 CheckBox/DropDownList
- ASP.NET中为GridView添加删除提示框的方法