GridView's NewValues and OldValues empty in the OnRowUpdating event

前端 未结 5 2109
南旧
南旧 2020-12-20 14:26

I have the GridView below. I am binding to a custom datasource in the code behind. It gets into the \"OnRowUpdating\" event just fine, but there are no NewValues or OldVal

5条回答
  •  青春惊慌失措
    2020-12-20 15:16

    Regarding on the GridView control's RowUpdating event problem, it is the expected behavior because when we do not associate GridView(or other ASP.NET 2.0 databound control) with DataSource control, it won't automatically query and fill the parameters collection of the updating/deleting/... events. In such cases, we need to manually extract the field values from the Template control.

    This is what says a Microsoft employee in here.

    In that case you can do it using the ExtractValuesFromCell method to make the NewValues collection yourself.

    EDIT:

    I found a piece of code in the comments of this blog:

    protected void OnRowEditing(object sender, GridViewEditEventArgs e)
    {     
           GridView gv = (GridView)sender;
           gv.EditIndex = e.NewEditIndex;
           gv.DataBind();
           ...
    }
    
    protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
    {
       GridView gv = (GridView)sender;
       for (int i = 0; i < gv.Columns.Count; i++)
       {
          DataControlFieldCell cell = gv.Rows[e.RowIndex].Cells[i] as DataControlFieldCell;
          gv.Columns[i].ExtractValuesFromCell(e.NewValues, cell, DataControlRowState.Edit, true);
       }
       // now you can use NewValues collection normally
    }
    

    Havent tested it, but seems to solve the problem, let me know if it did.

提交回复
热议问题