前台代码
<asp:templatefield headertext="选择"> <itemtemplate>< /itemtemplate><asp:checkbox id="CheckBox2" runat="server">< /asp:checkbox>< /asp:templatefield><asp:button id="Button1" onclick="Button1_Click" runat="server" text=" 全部选中"></asp:button><asp:button id="Button2" onclick="Button2_Click" runat="server" text=" 删除所选"></asp:button>
后台代码
protected void Button1_Click(object sender, EventArgs e) { if (Button1.Text == " 全部选中") { foreach (GridViewRow row in GridView1.Rows) { CheckBox CheckBox2 = (CheckBox)row.Cells[0].FindControl("CheckBox2"); CheckBox2.Checked = true; } Button1.Text = " 全部不选"; } else { foreach (GridViewRow row in GridView1.Rows) { CheckBox CheckBox2 = (CheckBox)row.Cells[0].FindControl("CheckBox2"); CheckBox2.Checked = false; } Button1.Text = " 全部选中"; } } protected void Button2_Click(object sender, EventArgs e) { foreach (GridViewRow row in GridView1.Rows) { CheckBox CheckBox2 = (CheckBox)row.Cells[0].FindControl("CheckBox2"); if (CheckBox2.Checked == true) { SqlConnection conn = Fun.InitConn(); SqlCommand comm = new SqlCommand(); comm.Connection = conn; comm.CommandText = "delete from Comment where Comment_ID= " + row.Cells[2].Text + ""; conn.Open(); comm.ExecuteNonQuery(); conn.Close(); } } }