大家帮我看下这段代码

来源:百度知道 编辑:UC知道 时间:2024/06/08 05:42:35
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex].Value.ToString());
string pwd = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[2].FindControl("TextBox1"))).Text.ToString();
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ceshi"]);
conn.Open();
SqlCommand cmd = new SqlCommand("update admin set pwd='" + pwd + "'where id=" + id + "", conn);
cmd.ExecuteNonQuery();
cmd.Connection.Close();
conn.Close();
this.GridView1.EditIndex = -1;
gbing();

}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex].Value.ToString());
SqlConnect

这个你恐怕不应该只取一次吧,你的更新和删除获取的主键又不一定相同,而且两个事件应该不是在同一个生命周期里的,为什么要只取一次呢。

这就没有更好的方法了,只能这样重复的取!!!
我倒觉得这也不算麻烦呀
----要么你单独写个获得主键的方法