我使用以下代码来点击复选框填充表格,但表格中没有变化
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkUpdate = (CheckBox)
GridView1.Rows[i].Cells[0].FindControl("chkSelect");
if (chkUpdate != null)
{
if (chkUpdate.Checked)
{
string strID = GridView1.Rows[i].Cells[1].Text;
SqlCommand cmd;
string str1 = "update app1 set p_id=0 where p_id='" + strID + "'";
cmd = new SqlCommand(str1, con);
cmd .ExecuteNonQuery ();
}
}
}
}
答案 0 :(得分:1)
尝试使用此代码
foreach (GridViewRows gdrv in GridView1.Rows)
{
CheckBox chkUpdate = (CheckBox)
gdrv.FindControl("chkSelect");
if (chkUpdate != null)
{
if (chkUpdate.Checked)
{
string strID = gdrv.Cells[1].Text;
SqlCommand cmd;
string str1 = "update app1 set p_id=0 where p_id='" + strID + "'";
cmd = new SqlCommand(str1, con);
con.open();
cmd .ExecuteNonQuery ();
con.close();
}
}
}
答案 1 :(得分:0)
我猜你在cmd .ExecuteNonQuery ();
调试时,您可以进入if(chkUpdate.Checked)
块吗?
答案 2 :(得分:0)
我认为查询中唯一的问题是“Where”条件...如果p_id是Int类型,那么你不必使用''...
所以声明必须是:
string str1 = "update app1 set p_id=0 where p_id=" + strID;