问题
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:
Maybe you should use
if(!IsPostBack){
//your code.
}
Is that you are looking for?
回答2:
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.
来源:https://stackoverflow.com/questions/651293/objectdatasource-created-twice-when-control-is-changed