C# 如何获取 ObjectDataSource 的行数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1896896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:42:34  来源:igfitidea点击:

How to get row count of ObjectDataSource

c#objectdatasourcerowcount

提问by Mostafa

Hello you all

大家好

How can i get row count of ObjectDataSouce ?

如何获得 ObjectDataSouce 的行数?

I use ObjectDataSource and DataList . I want show some thing to the user for example in a label when there are certain row returned by ObjectDataSource . One of situation is when there is no record .

我使用 ObjectDataSource 和 DataList 。当 ObjectDataSource 返回某些行时,我想向用户显示一些内容,例如在标签中。一种情况是没有记录时。

谢谢你 。

采纳答案by Watki02

I was looking for the same answer... Another solution I ended up using is the following: This is found on a .vb file behind an .aspx page. It handles the "selected" event of the datasource.

我一直在寻找相同的答案...我最终使用的另一个解决方案如下: 这是在 .aspx 页面后面的 .vb 文件中找到的。它处理数据源的“selected”事件。

Protected Sub ObjectDataSource1_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs) Handles ObjectDataSource1.Selected
    ' Select data for rowcount
    Dim dt As DataTable = e.ReturnValue
    ' Set row count label
    Me.lblCount.Text = dt.Rows.Count.ToString
End Sub

回答by Eilon

The ObjectDataSourcedoes not have a direct way to get the total row count. One of the reasons for this is that if all you want is the total row count then you don't need the data source at all! To get the row count just talk to your Business Logic Layer (BLL) and get the total rows:

ObjectDataSource没有直接的方式来获取总行数。这样做的原因之一是,如果您想要的只是总行数,那么您根本不需要数据源!要获取行数,只需与您的业务逻辑层 (BLL) 交谈并获取总行数:

MyBLL bll = new MyBLL();
int customerRowCount = bll.Customers.GetRowCount();

The ObjectDataSourcedoes have a SelectCountMethodthat can be used when data bound controls such as the GridViewneed to access the total row count. However, this is only used while also performing a Select operation. That is, there is no way to onlyget the row count. The row count is only used so that the data bound control can display a pager control - it is not used for anything else.

所述ObjectDataSource确实有一个SelectCountMethod当数据绑定控件可用于如GridView需要访问总行数。但是,这仅在执行 Select 操作时使用。也就是说,没有办法获取行数。行计数仅用于数据绑定控件可以显示分页控件 - 它不用于其他任何内容。

回答by Ed Bishop

You can achieve this very simply using a pager template e.g.

您可以使用寻呼机模板非常简单地实现这一点,例如

       <asp:DataPager PagedControlID="PagedControlId" PageSize="20" QueryStringField="QueryStringName" ID="InfoPager" runat="server">
           <Fields>
               <asp:TemplatePagerField>
                   <PagerTemplate>
                        Showing results 
                        <%=InfoPager.StartRowIndex + 1 %> 
                        to 
                        <%= (new []{(InfoPager.StartRowIndex + InfoPager.PageSize),InfoPager.TotalRowCount})
                                      .OrderBy(x => x)
                                      .First()%> 
                        of 
                        <%=InfoPager.TotalRowCount %>
                   </PagerTemplate>
               </asp:TemplatePagerField>
           </Fields>
       </asp:DataPager>

This will produce the text "Results x to y of z" including a check for the last page.

这将产生文本“Results x to y of z”,包括对最后一页的检查。

Cheers,

干杯,

Ed

埃德

回答by Andrew Huey

Found this here:

在这里找到这个:

bool bGetSelectCount;
protected void ObjectDataSource1_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
    if (bGetSelectCount)
        TextBox1.Text = e.ReturnValue.ToString(); 
}

protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
    bGetSelectCount = e.ExecutingSelectCount;
}

回答by vic

Protected Sub objItemsList_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs) Handles objItemsList.Selected
    lblMessage.Text = DirectCast(e.ReturnValue, DataTable).Rows.Count & " record(s) found"
End Sub

回答by calterras

In my case the ObjectDatasource is a dataset, so I get row number in objectdatasource_selected event like this

在我的情况下,ObjectDatasource 是一个数据集,所以我在 objectdatasource_selected 事件中得到行号,如下所示

e.ReturnValue.tables(0).rows.count.ToString 

回答by Flo

Protected Sub myODS_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs) Handles myODS.Selected
    Dim s As String = e.ReturnValue.ToString
    Dim rows As Integer
    Int32.TryParse(s, rows)

    'rows variable now holds the total number of results, not just what's displayed on the current gridview page
End Sub