如果在Page_Load()中调用DataBind(),则SqlDataSource不会执行更新 [英] If DataBind() is called inside Page_Load(), then SqlDataSource doesn’t perform updates

查看:125
本文介绍了如果在Page_Load()中调用DataBind(),则SqlDataSource不会执行更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将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不会执行更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆