熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> ASP編程 >> 正文

ASP.NET Gridview 使用checkbox刪除

2022-06-13   來源: ASP編程 

  方法一後台代碼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>


From:http://tw.wingwit.com/Article/program/ASP/201311/21679.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.