80

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

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

Item 1 | some info

Item 2 | some info

... and so on

1 and 2 being the counter.

Brian Webster
  • 27,545
  • 47
  • 143
  • 218
Jan W.
  • 1,701
  • 5
  • 26
  • 28
  • Possible duplicate of [ASP.NET repeater alternate row highlighting without full blown ](http://stackoverflow.com/questions/847806/asp-net-repeater-alternate-row-highlighting-without-full-blown-alternatingitemt) – Richard Ev Mar 01 '16 at 09:25

2 Answers2

162

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

<asp:repeater id="rptRepeater" runat="server">
    <itemtemplate>
        Item <%# Container.ItemIndex + 1 %>| <%# Eval("Column1") %>
    </itemtemplate>
    <separatortemplate>
        <br />
    </separatortemplate>
</asp:repeater>
Binoj Antony
  • 15,352
  • 24
  • 86
  • 95
6

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

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+"";
    }
TheVillageIdiot
  • 38,082
  • 20
  • 126
  • 184
  • +1 because I was looking for something like Item.ItemIndex in CodeBehind. I looked for Item.Index but no joy... – Resource Dec 04 '15 at 12:55