If DataBind() is called inside Page_Load(), then S

2019-05-31 06:31发布

问题:

If I bind GridView (via DataSourceID attribute) to SqlDataSource and set SelectCommand and UpdateCommand attributes, then everything works perfectly.

But I’ve noticed that if I, for whatever reason, also manually call DataBind() inside Page_Load(), then SqlDataSource doesn’t perform any updates, even though SqlDataSource.Updating and SqlDataSource.Updated events do fire when GridView’s Update button is clicked.

Could someone explain why updates don’t happen?

回答1:

It's because the Page_Load is fired BEFORE the SqlDataSource.Updating and SqlDataSource.Updated events fire. This means that the GridView resets to what it was before the user edits.

Please review the Page Lifecycle documentation for details. The SqlDataSource.Updating and SqlDataSource.Updated events happen in the Postback Event Handling section.