Method One:
Background code:
Copy Code code as follows:
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 (), "hint", "<script>alert" (' Delete succeeded! ') </script> ");
}
}
This. Gridview1.databind ();
}//Delete
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 ();
}
}
Foreground code:
Copy Code code as follows:
<asp:gridview id= "GridView1" runat= "Server" datakeynames= "ID" >
You also have to add a column to bind the field ID, and set the Visable property of this column to False
Method Two:
Background:
Copy Code code as follows:
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 (), "hint", "<script>alert" (' Delete succeeded! ') </script> ");
}
Else
{
Clientscript.registerstartupscript (GetType (), "hint", "<script>alert" (' Delete failed! ') </script> ");
}
This. DataBind ();
}
}
}
}
}
}
Front desk:
Copy Code code as follows:
<style type= "Text/css" >
. Hidden
{
Display:none;
}
</style>
<asp:boundfield datafield= "id" headertext= "number" >
<itemstyle cssclass= "Hidden"/>
</asp:BoundField>
Add a new column, this column binds the ID field, and the Visable property cannot be false, otherwise the value cannot be taken.
CheckBox Full selection Feature:
Copy Code code as follows:
<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)"/>
Select All
</HeaderTemplate>