asp.net - 控件更改时 ObjectDataSource 创建两次

标签 asp.net data-binding gridview

我正在将 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/

相关文章:

asp.net - 字 rune 字中的字符太多?

data-binding - DevExpress LookUpEdit 问题

pdf - kartik GridView Yii2 - 如何配置 PDF 的导出配置(页眉、页脚、标题)

c# - 静态变量以某种方式维护状态?

c# - ASP.NET 中的 P/Invoke(从 dll 读取/写入文本文件)

c# - "System.ServiceModel.Primitives"- 找不到程序集或找不到依赖项

.net - 取消数据绑定(bind)控件验证事件不会抑制更新数据源的尝试

保存前 WPF 数据绑定(bind)

android - ScrollView 不适用于 GridView 和线性布局

c# - 选择下拉列表时无法阻止 gridview 刷新