在GridView中单击更新按钮时会触发RowEditing

时间:2014-04-10 07:12:36

标签: c# asp.net gridview webforms

我创建了一个GridView,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    GridView gv = new GridView();
    gv.ID = pId.ToString();
    gv.AutoGenerateEditButton = true;
    gv.DataKeyNames = ids;
    gv.RowEditing += gv_RowEditing;
    gv.RowUpdating += gv_RowUpdating;
    bindGv(pId, gv);
}

我还写了以下方法: -

RowUpdating

void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridView gv = sender as GridView;
    GridViewRow row = (GridViewRow)gv.Rows[e.RowIndex];
    ProductCategory pc = context.ProductCategories.First(s => s.Name ==gv.ID );
    TextBox txtName = row.FindControl("txtName") as TextBox;
    pc.Name = txtName.Text;
}

RowEditing

void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView gv = sender as GridView;
    gv.EditIndex = e.NewEditIndex;
    bindGv(Convert.ToInt32(gv.ID), gv);
}

但是当我在调试模式下运行代码时,单击更新按钮会调用gv_RowEditing方法而不是gv_RowUpdating。有什么问题?

1 个答案:

答案 0 :(得分:0)

当您单击编辑按钮以更新任何称为rowediting事件的记录时触发gridview事件的序列,之后当您单击更新按钮时它会调用rowupdating事件。 rowediting事件总是首先调用然后rowupdating。