2013-02-01 145 views
3

当我输入新数据并按下更新按钮时,它会保存旧数据(我希望它更新的数据)。asp.net gridview编辑按钮不更新

public void fill() 
{ 
    SqlCommand cmd = new SqlCommand("select * from school ",con); 
    con.Open(); 
    SqlDataReader rd = cmd.ExecuteReader(); 

    GridView1.DataSource = rd; 
    GridView1.DataBind(); 
    con.Close(); 
} 

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) 
{ 
    fill(); 
} 

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 

    int id = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text); 
    string stu =((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text; 
    int age = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text); 

    SqlCommand cmd = new SqlCommand("UPDATE school SET [email protected]_name,[email protected] where [email protected] ", con); 
    cmd.Parameters.Add(new SqlParameter("@id", id)); 
    cmd.Parameters.Add(new SqlParameter("@stu_name", stu)); 
    cmd.Parameters.Add(new SqlParameter ("@age",age)); 

    con.Open(); 
    cmd.ExecuteNonQuery(); 
    con.Close(); 

    GridView1.EditIndex = -1; 
    fill(); 
} 

他的问题,分配给名字的数值,年龄都在数据库中现有的值不是我在运行时 任何一个能帮助我进入了新的价值观 在此先感谢

+0

你在page_load中有什么,你可以发布该代码? – Kiran1016

+0

欢迎来到SO。你在页面加载时写了什么? –

+0

人们仍然使用网络表单? –

回答

0

您每次编辑时重新填充网格。

改为在grid init上调用fill();

+0

没有解决问题, 问题是分配给name,age的值是数据库中的现有值 – moon

0

这是关于网页表格的Life Cycle的一些信息。我认为您需要的只是将fill();包装在if声明中。因为page_load发生在您的事件处理程序之前,所以您可以从输入的值之上的db重新加载。

if(!PostBack) 
{ 
    fill(); 
}