问题描述
如果我将GridView(通过DataSourceID属性)绑定到SqlDataSource并设置了SelectCommand和UpdateCommand属性,那么一切都可以正常工作。但是我注意到,如果我因为任何原因也在Page_Load()中手动调用DataBind(),那么SqlDataSource不会执行任何更新,即使SqlDataSource.Updating和SqlDataSource.Updated事件在GridView的更新按钮被点击时触发。
有人可以解释为什么不会发生更新?
这是因为在SqlDataSource.Updating和SqlDataSource.Updated事件触发之前,Page_Load被触发。这意味着GridView将重置为用户编辑之前的内容。
请查看文档以获取详细信息。 SqlDataSource.Updating和SqlDataSource.Updated事件发生在Postback事件处理部分。
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?
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.
这篇关于如果在Page_Load()中调用DataBind(),则SqlDataSource不会执行更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!