如何获取ObjectDataSource的行数

发布于 2024-08-14 18:20:52 字数 172 浏览 11 评论 0原文

大家好,

我如何获取 ObjectDataSouce 的行数?

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

Thank you .

Hello you all

How can i get row count of 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 .


Thank you .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

丑丑阿 2024-08-21 18:20:52

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

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

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.

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
<逆流佳人身旁 2024-08-21 18:20:52

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

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

ObjectDataSource 确实有一个 SelectCountMethod,当数据绑定控制此类时,可以使用该方法。因为 GridView 需要访问总行数。然而,这仅在执行选择操作时使用。也就是说,无法获取行数。行计数仅用于数据绑定控件可以显示分页器控件 - 它不用于其他任何用途。

The ObjectDataSource does 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:

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

The ObjectDataSource does have a SelectCountMethod that can be used when data bound controls such as the GridView need to access the total row count. However, this is only used while also performing a Select operation. That is, there is no way to only get 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.

_畞蕅 2024-08-21 18:20:52

此处找到了这个:

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

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;
}
微暖i 2024-08-21 18:20:52

您可以使用寻呼机模板非常简单地实现此目的,例如,

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

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

干杯,

艾德

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.

Cheers,

Ed

灵芸 2024-08-21 18:20:52
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
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
心碎无痕… 2024-08-21 18:20:52

就我而言,ObjectDatasource 是一个数据集,因此我在 objectdatasource_selected 事件中获取行号,如下所示

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

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

e.ReturnValue.tables(0).rows.count.ToString 
寒冷纷飞旳雪 2024-08-21 18:20:52
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文