
飘过~~
批量更新吗?
看样子学号应该是唯一值
设置为DATAKEY
如果做批量更新这样用
protected void Button1_Click(object sender, EventArgs e)
{
foreach (DataGridItem i in DataGrid1.Items)
{
TextBox txt = (TextBox)i.FindControl("TextBox2");
string text = txt.Text.ToString();
string strDelId = DataGrid1.DataKeys[i.RowIndex].Value.ToString();
if (txt.Text == null)
{
Response.Write("<script defer>alert('不能为空!');</script>");
}
else
{
string str_mark = "update lab_students set mark='" + text + "' where user_ID='" + strDelId + "'";
co_m.ENQ(str_mark);
Response.Write("<script defer>alert('输入成功!');</script>");
}
}
}
你试了看看吧,我不太记得datagrid的方法了,用2005习惯了,如果出错在贴出来
为什么你老是想这样写呢,Cells[1]这已经把行的索引给固定了,而且更新根本就是错误的,只是行的索引,用来作为更新的条件,怎么可能是对的呢。
你该设置一个DataKey,在页面设计的DataGrid上右键,属性,找到DataKey,然后设置为学号,我这里设想学号是唯一值,把他当做主键吧。
你1楼的语法是正确的,仔细看了一下没有什么错误,但是没有更新是因为你的条件根本不成立,自然就不能更新数据。
那么只能借助DataKey来做为条件,用foreach来迭代,进行重复的UPDATE更新。
如果你固定了行。那么只是更新其中一行,其他的忽略,这样foreach还有意义么?