开发者

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

开发者 https://www.devze.com 2023-01-01 17:22 出处:网络
I have an object datasource that looks like this: <asp:ObjectDataSource ID=\"obdsList\" runat=\"server\"

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 lik开发者_如何学JAVAe a hack.


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
        }                
    }
}


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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消