我正在将 ObjectDataSource 与 GridView 一起使用,并在代码隐藏中使用 OnObjectCreated 处理程序。如果我以编程方式更改 GridView 上的子控件值,则整个控件会在同一请求中再次进行数据绑定(bind)(如 OnObjectCreated 处理程序所示),这是我不希望的。这发生在初始页面 GET 请求上(因此这不是回发问题)。以下是跟踪显示的内容:
aspx.page Begin PreRender
Custom IN handleDSObjectCreated() => tsDao: ETime.DAL.TimeSheetDAO
Custom OUT handleDSObjectCreated()
Custom IN handleDSObjectCreated() => tsDao: ETime.DAL.TimeSheetDAO
Custom OUT handleDSObjectCreated()
aspx.page End PreRender
即使我操作子控件,是否有办法阻止第二轮数据绑定(bind)?数据层中没有任何变化,因此不需要它。另请注意,不涉及图像网址,这似乎也会导致双重数据绑定(bind)。谢谢。
更新:
我不确定这是否有帮助,但在调用 OnObjectCreated 处理程序时查看堆栈跟踪显示以下差异:
第一次事件处理程序调用:
System.Web.dll!System.Web.UI.WebControls.GridView.DataBind() + 0x5 bytes
System.Web.dll!System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() + 0x53 bytes
System.Web.dll!System.Web.UI.WebControls.GridView.OnPreRender(System.EventArgs e = {System.EventArgs}) + 0x19 bytes
System.Web.dll!System.Web.UI.Control.PreRenderRecursiveInternal() + 0x57 bytes
第二次事件处理程序调用:
System.Web.dll!System.Web.UI.WebControls.GridView.DataBind() + 0x5 bytes
System.Web.dll!System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() + 0x53 bytes
System.Web.dll!System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() + 0x46 bytes
System.Web.dll!System.Web.UI.Control.EnsureChildControls() + 0x58 bytes
System.Web.dll!System.Web.UI.Control.PreRenderRecursiveInternal() + 0x33 bytes
同样,这全部来自最初的 GET 请求。知道为什么它被调用两次吗?
最佳答案
也许你应该使用
if(!IsPostBack){
//your code.
}
这是您要找的吗?
关于asp.net - 控件更改时 ObjectDataSource 创建两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/651293/