【问题标题】:How do I tell if the ObjectDataSource OnSelected event was called for the selectmethod or selectcountmethod?如何判断是否为 selectmethod 或 selectcountmethod 调用了 ObjectDataSource OnSelected 事件?
【发布时间】:2010-05-26 18:09:29
【问题描述】:

我有一个如下所示的对象数据源:

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

还有一个这样的 onselected 事件:

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

但是,事件方法被调用了两次。一次是使用我返回的列表,一次是使用返回的 Int32 计数。如果我想将e.ReturnValue 转换为返回列表,我该如何区分计数和选择方法?我可以做一个e.ReturnValue.GetType().ToString() 但这似乎是一个hack。

【问题讨论】:

    标签: c# asp.net c#-3.0 objectdatasource


    【解决方案1】:

    我正在这样做……

    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】:

    From MSDN:

    的 ExecutingSelectCount 属性 ObjectDataSourceSelectingEventArgs 对象用于确定是否 调用 select 来检索数据或检索计数。

    所以我相信您需要检查 OnSelecting 事件,而不是 OnSelected 事件。即:

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

    但如果你真的需要在 OnSelected 事件中使用它,那么你可能需要将 e.ExecutingSelectCount 临时存储在某个地方,或者......继续检查我猜的结果类型......

    【讨论】:

    【解决方案3】:

    我最近遇到了这个问题,通过一系列晦涩的搜索,发现我看到第二次执行(在我的 ObjectDataSource 中指定的SelectMethodSelectCountMethod)的原因是改变了数据绑定发生后,gridview 中的列。事实证明,在数据绑定后对 gridview 中显示的列所做的任何更改都会导致 ObjectDataSource 重新执行这两个方法。

    在我的例子中,我能够将列可见性代码移动到 gridview.DataBind() 调用前面,并且第二组执行停止。但是,如果您的可见性更改取决于数据绑定检查的结果,这可能是不可能的。在这种情况下,您将不得不更加复杂和创造性地处理第二次执行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-05
      • 1970-01-01
      • 1970-01-01
      • 2019-10-12
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多