gridview中有个checkbox,我想在gridview外点“全选”钮,所有的checkbox都打上钩选中
发布网友
发布时间:2022-04-27 12:22
我来回答
共2个回答
热心网友
时间:2022-04-27 13:52
全选
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (CheckBox2.Checked == true)
{
cbox.Checked = true;
}
else
{
cbox.Checked = false;
}
}
}
删除
protected void Button2_Click(object sender, EventArgs e)
{
sqlcon = new SqlConnection(strCon);
SqlCommand sqlcom;
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (cbox.Checked == true)
{
string sqlstr = "delete from 飞狐工作室 where 身份证号码='" + GridView1.DataKeys[i].Value + "'";
sqlcom = new SqlCommand(sqlstr, sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
}
}
bind();
}
热心网友
时间:2022-04-27 15:10
下面是写的全选的一个方法,看对你有用吗?
protected void checkall_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
if (chk.Checked)
{
for (int i = 0; i < gridview1.Rows.Count; i++)
{
((CheckBox)gridview1.Rows[i].Cells[8].FindControl("chk1")).Checked = true;
}
}
else
{
for (int i = 0; i < gridview1.Rows.Count; i++)
{
((CheckBox)gridview1.Rows[i].Cells[8].FindControl("chk1")).Checked = false;
}
}
}