积极答复者
GridView编辑功能问题

问题
-
用GridView控件自带的“编辑功能,为什么下划线部分得到的值仍然是更改之前的值?初学者,求各位帮忙看一下。谢谢。
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { int testItemId = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString()); string testItemname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text; active a = new active(); a.updateInfo(testItemname, testItemId); e.Cancel = true; GridView1.EditIndex = -1; GridView1.DataSource = a.getInfo(); GridView1.DataBind(); }
答案
全部回复
-
string testItemname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
1、检查上面一行中的Cells[0].Controls[0]是否为你编辑的TextBox
2、检查active类中的updateInfo方法是否有误,updateInfo方法应该类似如下写法
public void updateInfo(string testItemname,string testItemId) { string sql_Cmd="update 表 set 你要更新的字段名='"+testItemname+"' where 主键='"+testItemId+"'"; using (SqlConnection conn = new SqlConnection("你的连接"))//这里如果不加“你的连接”一定更新不成功 { using (SqlCommand cmd = new SqlCommand(sql_Cmd,conn))//同样,这里如果不加“conn”的话也更新不成功 { conn.Open(); if (cmd.ExecuteNonQuery() > 0) { ScriptManager.RegisterClientScriptBlock(this.Page, typeof(Page), "", "alert('更新成功')", true); } } } }
检查代码中提到的两处数据库连接是否都被加上。