C# ASP.net Repeater 获取当前索引、指针或计数器

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

ASP.net Repeater get current index, pointer, or counter

c#asp.netrepeater

提问by Jan W.

the question is really simple. Is there a way to access the current pointer/counter for an asp Repeater control.

问题很简单。有没有办法访问 asp 中继器控件的当前指针/计数器。

I have a list with items and I would like one of the repeaters columns (it repeats and html table) to be something like ...

我有一个包含项目的列表,我希望其中一个中继器列(它重复和 html 表)类似于...

Item 1 | some info

第 1 项 | 一些信息

Item 2 | some info

第 2 项 | 一些信息

... and so on

... 等等

1 and 2 being the counter.

1 和 2 是计数器。

采纳答案by Binoj Antony

To display the item number on the repeater you can use the Container.ItemIndexproperty.

要在中继器上显示项目编号,您可以使用该Container.ItemIndex属性。

<asp:repeater id="rptRepeater" runat="server">
    <itemtemplate>
        Item <%# Container.ItemIndex + 1 %>| <%# Eval("Column1") %>
    </itemtemplate>
    <separatortemplate>
        <br />
    </separatortemplate>
</asp:repeater>

回答by TheVillageIdiot

Add a label control to your Repeater's ItemTemplate. Handle OnItemCreated event.

将标签控件添加到中继器的 ItemTemplate。处理 OnItemCreated 事件。

ASPX

ASPX

<asp:Repeater ID="rptr" runat="server" OnItemCreated="RepeaterItemCreated">
    <ItemTemplate>
        <div id="width:50%;height:30px;background:#0f0a0f;">
            <asp:Label ID="lblSr" runat="server" 
               style="width:30%;float:left;text-align:right;text-indent:-2px;" />
            <span 
               style="width:65%;float:right;text-align:left;text-indent:-2px;" >
            <%# Eval("Item") %>
            </span>
        </div>
    </ItemTemplate>
</asp:Repeater>

Code Behind:

背后的代码:

    protected void RepeaterItemCreated(object sender, RepeaterItemEventArgs e)
    {
        Label l = e.Item.FindControl("lblSr") as Label;
        if (l != null)
            l.Text = e.Item.ItemIndex + 1+"";
    }