views:

394

answers:

2

I'm using an ObjectDataSource with a GridView with an OnObjectCreated handler on the code-behind. If I programmatically change a child control value on the GridView, the entire control gets databound a second time in the same request (as shown by the OnObjectCreated handler), which I don't want. This happens on the initial page GET request (so it is not a postback issue). Here's what a trace shows:

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

Is there a way to prevent the second round of databinding even if I manipulate the child controls? Nothing in the data layer changes so it is not needed. Note also there are no image urls involved which seem to cause double databinding too. Thanks.

Update:

I'm not sure if this helps or not, but in looking at the stack trace at the point at which the OnObjectCreated handler is called shows the following differences:

First Event Handler Invocation:

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

Second Event Handler Invocation:

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

Again, this is all from the initial GET request. Any ideas why it's getting invoked twice?

+1  A: 

Maybe you should use

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

Is that you are looking for?

nandokakimoto
Unfortunately no. It happens on the inital GET request.
A: 

Is the fact that databinding occurs twice in your scenario causing other issues? Just fyi, you can manage the data source object creation yourself by handling the ObjectCreating event. If you set the ObjectDataSourceEventArgs.ObjectInstance property in the handler to a valid (non-null) instance, the data source will bind to that instance rather than creating a new one.

Daniel Pratt
The concern is unnecessarily hitting the database twice on every request--not necessarily the creation of the duplicate datasource. I could save off the instance and cache the result, but I'd rather solve the core problem.