views:

148

answers:

3

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.

+1  A: 

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

Homer
Yeah, it turned out to be just that, we're hiding/showing columns in the GridView.DataBound event, and if we don't do that, it's doesn't fire twice. I'll check out your link, but which method did you use to solve the situation?
LoveMeSomeCode
We moved the hiding/showing from DataBound to Page_Load.
Homer
A: 

"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

Tomas Voracek
no, it looks like Page_Load is only being called once. Thanks for the link though, I'll take a look.
LoveMeSomeCode
A: 

If you set the datasource of the gridview with something like

gv.DataSourceID=dsObjDataSource;

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

mikek3332002
we're doing that in the aspx, but not in the code
LoveMeSomeCode
Makes no difference which of the 2 files sets that property(aspx and aspx.cs)
mikek3332002