asp.net

位置:IT落伍者 >> asp.net >> 浏览文章

ASP.NET Gridview 使用checkbox删除


发布日期:2022年05月29日
 
ASP.NET Gridview 使用checkbox删除

方法一后台代码protected void btn_delete_Click(object sender EventArgs e)

{ for (int i = i <thisGridViewRowsCount i++)

{ int id = ConvertToInt(thisGridViewDataKeys[i]Value)if ((thisGridViewRows[i]Cells[]FindControl(CheckBox) as CheckBox)Checked == true)

{ Delete(id)ClientScriptRegisterStartupScript(GetType()提示<script>alert(删除成功!)</script>} thisGridViewDataBind()}//删除private void Delete(int id)

{ using (SqlConnection conn = new SqlConnection(str))

{ connOpen()SqlCommand comm = connCreateCommand()commCommandText = delete from Notice_Msg where id=@idcommParametersAdd(new SqlParameter(@id id))commExecuteNonQuery()}前台代码<aspGridView ID=GridView runat=server DataKeyNames=id>另外还得添加一列让其绑定的字段为id并且把这一列的visable属性设为false方法二后台protected void btn_delete_Click(object sender EventArgs e)

{ foreach (GridViewRow row in thisGridViewRows)

{ if (rowRowType == DataControlRowTypeDataRow)

{ CheckBox ckb = rowCells[]FindControl(CheckBox) as CheckBoxif (ckbChecked)

{ using (SqlConnection sqlCnn = new SqlConnection(str))

{ using (SqlCommand sqlCmm = sqlCnnCreateCommand())

{ sqlCmmCommandText = delete from Regime_Table where id= + rowCells[]Text + sqlCnnOpen()int a= sqlCmmExecuteNonQuery()if (a>

{ ClientScriptRegisterStartupScript(GetType()提示<script>alert(删除成功!)</script>} else { ClientScriptRegisterStartupScript(GetType() 提示 <script>alert(删除失败!)</script>} thisDataBind()}前台<style type=text/css>Hidden { displaynone} </style> <aspBoundField DataField=id HeaderText=编号 > <HeaderStyle CssClass=Hidden /> <ItemStyle CssClass=Hidden /> </aspBoundField>新增加一列这一列绑定id字段并且visable属性不能为false否则取不出值来

checkbox全选功能<script type=text/jscript> function change(sender) { var table = documentgetElementById(GridViewfor (var i = i < tablerowslength i++) { tablerows[i]cells[]getElementsByTagName(input)[]checked = senderchecked} </script> <HeaderTemplate> <input id=Checkbox type=checkbox onclick=change(this)/>全选</HeaderTemplate>

               

上一篇:深入ASP.NET数据绑定(上)

下一篇:怎样在ASP.NET中创建用户控件