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


Answers:


160

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

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

是的,这就是窍门。感谢您的帮助,您必须知道在哪里看:)
Jan W.

感谢您的有用提示。:)
Sedat Kumcu

6

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

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>

背后的代码:

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

+1是因为我在CodeBehind中寻找类似Item.ItemIndex的东西。我在寻找Item.Index但没有喜悦...
资源
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.