GridView_RowUpdating返回旧值

时间:2014-07-17 07:27:14

标签: asp.net gridview event-handling

我有一个网格视图,其中有更新/删除行的功能。我使用存储过程将数据存储到SQL。当我单击“编辑”按钮并更改现有值时,单击“更新”按钮后,我将获得旧值。

我的代码是:

protected void grdNatureFormation_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
{
    ConnectionString = GetConnectionString();            
    LinkButton link = (LinkButton)grdNatureFormation.Rows[e.RowIndex].FindControl("btnUpdate");
    int id = Convert.ToInt32(link.CommandArgument);
    TextBox title = (TextBox)grdNatureFormation.Rows[e.RowIndex].FindControl("txtTitle");
    if(title != null)
    {
        using (SqlConnection Sqlcon = new SqlConnection(ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand())                
            {
                Sqlcon.Open();
                cmd.Connection = Sqlcon;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "NatureOfFormation";
                cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int)).Value = id;
                cmd.Parameters.Add(new SqlParameter("@Title", SqlDbType.VarChar)).Value = title.Text.Trim();
                cmd.Parameters.Add(new SqlParameter("@Action", SqlDbType.VarChar)).Value = "update";
                cmd.ExecuteNonQuery();
                grdNatureFormation.EditIndex = -1;
                LoadData();
            }
        }
    }            
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        LoadData();
    }
}

private void LoadData()
{
    ConnectionString = GeneralMethods.GetConnectionString();
    SqlConnection Sqlcon = new SqlConnection(ConnectionString);
    SqlCommand cmd = new SqlCommand();
    try
    {
        Sqlcon.Open();
        cmd.Connection = Sqlcon;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "xxx";
        cmd.Parameters.Add(new SqlParameter("@Action", SqlDbType.VarChar, 50));
        cmd.Parameters["@Action"].Value = "select";
        SqlAda = new SqlDataAdapter(cmd);
        ds = new DataSet();
        SqlAda.Fill(ds);
        grdNatureFormation.DataSource = ds;
        grdNatureFormation.DataBind();
    }
    catch
    {
    }
    finally
    {
        if (Sqlcon.State == ConnectionState.Open)
            Sqlcon.Close();
        Sqlcon.Dispose();
        cmd.Dispose();
    }

}

我在互联网上搜索了这个问题,大多数帖子建议将数据绑定方法放在(!Page.IsPostBack)中,但在我的情况下,它已经处于相同的状态但没有获得价值。

我应该怎样做才能在RowUpdating事件中获得新价值?

1 个答案:

答案 0 :(得分:0)

RowUpdating在之前被称为更新gridview值。您需要将代码放入RowUpdated事件处理程序。

有关详细信息,请参阅手册:GridView Events

RowUpdating - 单击行的“更新”按钮但在GridView控件更新行之前发生。

RowUpdated - 单击行的“更新”按钮但在GridView控件更新行后发生。