vb.net 如何计算asp中继器中的项目数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23565308/
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-09-17 17:32:21  来源:igfitidea点击:

How to count the number of items in an asp repeater?

asp.netvb.netdata-bindingrepeatermarkup

提问by DreamTeK

How to count the number of items in a repeater using ASP.NET?

如何使用 ASP.NET 计算中继器中的项目数?

I would like to display something along the lines of:

我想显示以下内容:

MARKUP

标记

Page 1 of 5

第 1 页,共 5 页

ASPX

ASPX

Get Item Index: (working)

获取项目索引:(工作)

<%# DataBinder.Eval(Container, "ItemIndex", "") + 1%>

Count total items: (failing)

计算总项目数:(失败)

<%# DataBinder.Eval(Container.ItemIndex, rpt.Items.Count)%>


I would prefer to do this on page however am open to suggestions and code behind.

我更愿意在页面上执行此操作,但我愿意接受建议和背后的代码。

采纳答案by Ahmad Joumah

This is the best solution I get it

这是我得到的最好的解决方案

<asp:Repeater ID="rptGallery" runat="server"  >
    <ItemTemplate>
        <div>
            <div>        
                <asp:Label ID="Number_of_element_in_repeater" runat="server"
                Text='<%#Convert.ToString(((IList((Repeater)Container.Parent).DataSource).Count) %>'>
                </asp:Label>

            </div>
        </div>
    </ItemTemplate>
</asp:Repeater>


The number of items in repeater is:

中继器中的项目数为:

Convert.ToString(((IList)((Repeater)Container.Parent).DataSource).Count)

回答by ewitkows

I believe a SqlDataSource uses a "SQLdatareader", which is one by one row access. So if it has rows, it returns one row at a time, not knowing how much more it has to go.

我相信 SqlDataSource 使用“SQLdatareader”,它是逐行访问的。所以如果它有行,它一次只返回一行,不知道还有多少行。

You could include in your rowset the total amount of rows (something like count(*)), and return that as a field in each row. It's not ideal, but it's one way to return it to your current code.

您可以在行集中包含总行数(类似于 count(*)),并将其作为每行中的一个字段返回。这并不理想,但这是将其返回到当前代码的一种方法。

If you're not opposed to using the codebehind instead\as well, you could call out and fetch the records to get a rowcount that way, and then use that value in your markup (setting it as a hidden field, for example). Either way, you'll need to iterate through all rows before you know how many rows you have.

如果您也不反对使用代码隐藏,那么您可以调用并获取记录以通过这种方式获取行数,然后在标记中使用该值(例如,将其设置为隐藏字段)。无论哪种方式,您都需要遍历所有行才能知道有多少行。

回答by Duned

It looks like you need to cast to a data type that supports the properties you are using, try this:

看起来您需要转换为支持您正在使用的属性的数据类型,试试这个:

((Container.Parent as Repeater).DataSource as IList).Count