Search code examples
asp.netlistviewobjectdatasourcedatapager

SelectMethod in objectDatasource getting called multiple times with multiple datapagerfield


Ok, so here is the setup. I am building a page that has a listview, a datapager, and 3 datapagerfield (2 x NextPreviousPagerField, 1 x NumericPagerField), and a objectdatasource to tide all of this together.

It was all working fine until I put a breakpoint into the SelectMethod specified in the objectdatsource control. It seems like that for each datapagerfield control, it is calling the selectmethod and selectcount method. Hence, whenever a user paged, it calls the database 6 times instead of 2 (I don't have caching turned on atm). If I remove one datapagerfield, it will remove 2 calls.

Now this is build in asp.net 3.5 SP1 in VS2008. When I copied the same code files to a asp.net 4.0 VS2010 solution, it duplicate call seems to be gone.

Is this a bug in asp.net 3.5 SP1?

Thanks in advance


Solution

  • Actually you should be using the OnSelecting event.

    What happens is that ObjectDataSource calls the method SelectMethod twice

    1. First time it gets the data.
    2. Next time it gets the count.

    So I think you have to implement the OnSelecting event

    <asp:ObjectDataSource ID="ODS" runat="server" SelectMethod="GetList" SelectCountMethod="GetListCount" 
        OnSelecting="ods_Selecting">
        TypeName="Website.Test" EnablePaging="true" /> 
    

    and then cancel the event when the ObjectDataSource tries to call the count method.

     protected void ods_Selecting(object sender,
                    ObjectDataSourceSelectingEventArgs e)
     {
          if (e.ExecutingSelectCount)
          {
               //Cancel the event   
               return;
          }
    }
    

    You can use the link below so that another db call is not made to fetch the count. http://www.unboxedsolutions.com/sean/archive/2005/12/28/818.aspx

    Hope this helps.