开发者

Due to changing the order of statements two postbacks are required instead of just one, before …

开发者 https://www.devze.com 2022-12-13 02:24 出处:网络
If we have the following code, then when user clicks an Edit button, page is posted back and put into Edit mode:

If we have the following code, then when user clicks an Edit button, page is posted back and put into Edit mode:

protected void gvwEmployees_RowEditing(object sender, GridVie开发者_如何学GowEditEventArgs e)
{
    gvwEmployees.EditIndex = e.NewEditIndex;

    gvwEmployees.DataSource = ds.Tables["Employees"];

    gvwEmployees.DataBind();
}

But with the following code, user has to click the Edit button twice before a row is put into edit mode ( thus page needs to be posted back twice before row gets into edit mode). Why does it matter whether gvwEmployees.EditIndex is assigned a value before or after we bind GridView to a data source?

protected void gvwEmployees_RowEditing(object sender, GridViewEditEventArgs e)
{
    gvwEmployees.DataSource = ds.Tables["Employees"];

    gvwEmployees.DataBind();

    gvwEmployees.EditIndex = e.NewEditIndex;
}

Thank you


Modifying the EditIndex property with a value different than the one it already has requires that DataBind() is called after the modification.

As described in the GridView.EditIndex documentation page, it could also happen if EditIndex is modified under other circumstances:

If you set the EditIndex property after a postback or in handlers for events that are raised later than the Load event, the GridView control might not enter edit mode for the specified row. If you read the value of this property in other event handlers, the index is not guaranteed to reflect the row that is being edited.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号