今天做了全选与全不选,在gridview中选择编列选择templatefield然后在编辑模板其中有两个地方可以插入checkbox 在itemtemplate与headertemplated(中用html中的input checkbox)
然后在前台区增加一个方法
<input id="Checkbox2" type="checkbox" οnclick="change(this)" />
<script type="text/javascript">
function change(sender) {
var table = document.getElementById("GridView1");
for (var i = 1; i < table.rows.length; i++) {
table.rows[i].cells[0].getElementsByTagName("input")[0].checked = sender.checked;
}
}
</script>
全选或都部分选择,实现删除功能
foreach (GridViewRow row in this.GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chb = row.Cells[0].FindControl("CheckBox1") as CheckBox;
if (chb.Checked)
{
string str = ConfigurationManager.ConnectionStrings["cnnstring"].ConnectionString;
using (SqlConnection sqlcnn = new SqlConnection(str))
{
using (SqlCommand sqlcmm = sqlcnn.CreateCommand())
{
sqlcmm.CommandText = "delete from guizhangzhidu where id='" + row.Cells[1].Text + "'";
sqlcnn.Open();
int a = sqlcmm.ExecuteNonQuery();
if (a > 0)
{
Response.Write("删¦?除y成¨¦功|!ê?");
}
else
{
//ClientScript.RegisterStartupScript(GetType(), "提¬¨¢示º?", "<script>alert('删¦?除y失º¡ì败㨹!ê?')</script>");
Response.Write("删¦?除y失º¡ì败㨹!ê?");
}
this.guizhang();
}
}
}
}
然后在前台区增加一个方法
<input id="Checkbox2" type="checkbox" οnclick="change(this)" />
<script type="text/javascript">
function change(sender) {
var table = document.getElementById("GridView1");
for (var i = 1; i < table.rows.length; i++) {
table.rows[i].cells[0].getElementsByTagName("input")[0].checked = sender.checked;
}
}
</script>
全选或都部分选择,实现删除功能
foreach (GridViewRow row in this.GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chb = row.Cells[0].FindControl("CheckBox1") as CheckBox;
if (chb.Checked)
{
string str = ConfigurationManager.ConnectionStrings["cnnstring"].ConnectionString;
using (SqlConnection sqlcnn = new SqlConnection(str))
{
using (SqlCommand sqlcmm = sqlcnn.CreateCommand())
{
sqlcmm.CommandText = "delete from guizhangzhidu where id='" + row.Cells[1].Text + "'";
sqlcnn.Open();
int a = sqlcmm.ExecuteNonQuery();
if (a > 0)
{
Response.Write("删¦?除y成¨¦功|!ê?");
}
else
{
//ClientScript.RegisterStartupScript(GetType(), "提¬¨¢示º?", "<script>alert('删¦?除y失º¡ì败㨹!ê?')</script>");
Response.Write("删¦?除y失º¡ì败㨹!ê?");
}
this.guizhang();
}
}
}
}