ObjectDataSource firing twice, or on its own

Can someone explain exactly how/when an ObjectDataSource fires? I have an ASP.NET page, with a GridView, which is referencing an ODS. I put a breakpoint in the method the ODS is using, and noticed it was firing twice.

I looked into the code and the answer seemed obvious at first. I had

    Page_Load()
    {
      if(!Page.IsPostBack)
      {
          MethodA();
          MethodB();
      }
    }

where MethodA and MethodB were both eventually calling gv.DataBind(). This made sense because I assume that each call to GridView.DataBind() would result in asking the ODS for data, and therefore running my data access method.

The weird thing is that when comment out the call to MethodA, it still fires twice. Checking the call stack shows the method being run first as a result of MethodB, and then again, with no trail except [External Code]. This mystery load does not happen when I let MethodA and MethodB both execute.

Any idea what’s going on here? Any idea what other code I might have that is asking the ODS for data? I’m starting to think all these ‘no code’ data controls are more obfuscation and BS than they’re worth.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I’ve experienced this problem when we were hiding/showing gridview column dynamically in code.

Here is a page that talks about some issues that might cause multiple Selects
http://forums.asp.net/t/1161164.aspx

Method 2

Multiple calls to a gridview’s databind method can occur implicitly if you’re changing the visibility, i.e., showing and/or hiding, the columns of a gridview tied to an object data source.

In this scenario, try encapsulating the show/hide gridview column code in the Page_LoadComplete event handler.

The Page_LoadComplete event handler is in a prime position in the page lifecycle to prevent multiple databind calls and still be effective as it is invoked after control change events (e.g., button click, drop down selected index changed, etc.) yet before the gridview databinding events.

Method 3

“when comment out the call to MethodA, it still fires twice”. So it will likely be Page_Load called twice. Probably you have AutoEventWireup=”true” and also registering event in code http://www.aspdeveloper.net/tiki-index.php?page=ASPFaqEventsDoubleFire

Method 4

If you set the datasource of the gridview with something like

gv.DataSourceID=dsObjDataSource;

then the grid view calls gv.DataBind() on its own.

Method 5

I had the same problem – the problem was that I was hiding/showing a column after, or during, databinding. Moving the hide/show code before the databinding stopped the binding from happening twice, which I suspect is the same effect as moving it to the Page_Load. In my case the databind was happening in response to a dropdown listbox change – doing the column add/remove before the DataBind() call fixed the twofer problem for me.

Method 6

I was getting the same result with DataBinding occuring twice using asp:DataList and asp:ObjectDataSource.

It turned out to be because I was using a UserControl in the select parameters:

<asp:ControlParameter Name="GroupID" Type="Int32" DefaultValue="-1"
    ControlID="UserControl1" PropertyName="SelectedGroupID" />

I am getting weary of UserControls. I can see how they might be productivity enhancement for 5th graders but they are a complete waste of time at a higher level.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x