更改控件时,ObjectDataSource创建两次

时间:2009-03-16 17:07:40

标签: asp.net data-binding gridview

我正在使用带有GridView的ObjectDataSource和代码隐藏的OnObjectCreated处理程序。如果我以编程方式更改GridView上的子控件值,整个控件将在同一请求中第二次获取数据绑定(如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

即使我操纵子控件,有没有办法阻止第二轮数据绑定?数据层中没有任何内容发生变化,因此不需要。请注意,没有涉及图像网址,这似乎也导致双数据绑定。感谢。

更新

我不确定这是否有帮助,但是在调用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请求。任何想法为什么它被调用两次?

2 个答案:

答案 0 :(得分:2)

也许你应该使用

if(!IsPostBack){
 //your code.
}

你在寻找吗?

答案 1 :(得分:0)

在你的场景中数据绑定是否会发生两次导致其他问题?只需fyi,您就可以通过处理ObjectCreating事件来自己管理数据源对象的创建。如果将处理程序中的ObjectDataSourceEventArgs.ObjectInstance属性设置为有效(非null)实例,则数据源将绑定到该实例,而不是创建新实例。