简体   繁体   中英

How do I tell if the ObjectDataSource OnSelected event was called for the selectmethod or selectcountmethod?

I have an object datasource 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 a onselected event like this:

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

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

From MSDN :

The ExecutingSelectCount property of the ObjectDataSourceSelectingEventArgs object is used to determine if select was called to retrieve data or retrieve the count.

So I believe you need to check in the OnSelecting event, not the OnSelected event. ie:

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

But if you really need it on the OnSelected event, then you might need to temporarily store e.ExecutingSelectCount somewhere, or...just keep checking the result type I guess...

I'm doing this...

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

I ran into this issue recently and through a series of obscure searches, found that the reason I was seeing a second execution (of both the SelectMethod and the SelectCountMethod specified in my ObjectDataSource) was from changing the visibility of a column in the gridview after databinding had already occurred. It turns out that any changes made to the columns shown in the gridview after it is databound will cause the ObjectDataSource to re-execute both methods.

In my case, I was able to move the column-visibility code in front of the gridview.DataBind() call and the second set of executions ceased. This may not be possible, however, if your visibility changes are dependent on the results of a databound check. In that case, you'll have to get a bit more complicated and creative with how to handle the second execution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM