How to determine if an ObjectDataSource OnSelected event was raised for a selectmethod or selectcountmethod?

I have an object data source that looks like this:

<asp:ObjectDataSource ID="obdsList" runat="server" EnablePaging="True" SelectCountMethod="GetCountByID" SortParameterName="sortExpression" OldValuesParameterFormatString="original_{0}" SelectMethod="GetByID" TypeName="Services.Users" onselected="obdsList_Selected"> <SelectParameters> <asp:QueryStringParameter Name="ID" QueryStringField="ID" Type="Int32" /> </SelectParameters> </asp:ObjectDataSource> 

And an onselected event like this:

 protected void obdsList_Selected(object sender, ObjectDataSourceStatusEventArgs e) { } 

However, the event method is called twice .. once with my returned list and once with the returned number of Int32. If I want to pass e.ReturnValue to the return list, how can I distinguish between count and select methods? I can do e.ReturnValue.GetType().ToString() , but this seems like a hack.

+1
source share
3 answers

I'm doing it...

 protected void obdsList_Selected(object sender, ObjectDataSourceStatusEventArgs e) { if (e.ReturnValue != null) { if (e.ReturnValue.GetType() == typeof(int)) { //e.ReturnValue is the SelectCountMethod value } } } 
+2
source

From MSDN :

The ExecutingSelectCount property for the ObjectDataSourceSelectingEventArgs object is used to determine whether select was called to retrieve data or obtain an invoice.

Therefore, I believe that you need to check the OnSelecting event, not the OnSelected event. i.e:

 protected void ods_Selecting(object sender, ObjectDataSourceSelectingEventArgs e) { if (e.ExecutingSelectCount) { //select count method is being called } } 

But if you really need this in the OnSelected event, you may need to temporarily save the e.ExecutingSelectCount somewhere or ... just keep checking the type of the result, I think ...

+2
source

I recently ran into this problem and through a series of obscure searches I found that the reason I saw the second execution (both SelectMethod and SelectCountMethod specified in my ObjectDataSource) was because the column visibility changed in the gridview after the data binding had already occurred. It turns out that any changes made to the columns displayed in the gridview after binding it will cause the ObjectDataSource to re-execute both methods.

In my case, I managed to move the column visibility code before calling gridview.DataBind() , and the second set of executions stopped. However, this may not be possible if your visibility changes depend on the results of the data validation. In this case, you will have to complicate and orient a little in how to process the second execution.

+1
source

All Articles