展会信息港展会大全

GridView实现全选及删除源代码
来源:互联网   发布日期:2015-10-03 10:47:46   浏览:1032次  

导读:前台代码asp:templatefield headertext=选择 itemtemplate itemtemplateasp:checkbox id=CheckBox2 runat=server asp:checkbox asp:...

前台代码

<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(); } } }

赞助本站

人工智能实验室

相关热词: 全选 删除 GridView

相关内容
AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港