SelectMethod in objectDatasource getting called multiple times with multiple datapagerfield

二次信任 提交于 2019-12-01 11:12:13

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/3885073/selectmethod-in-objectdatasource-getting-called-multiple-times-with-multiple-dat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!