What is the easiest way to put an index to a repeater control in .NET?

18,188

Try the following:

<asp:Repeater ID="myRepeater" runat="server">
    <HeaderTemplate>
        <table>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td><%# Container.ItemIndex %></td>
            <!-- or maybe -->
            <td><%# Container.ItemIndex + 1 %></td>
            <td><%# DataBinder.Eval(Container, "DataItem.Name") %></td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>
Share:
18,188
Barış Velioğlu
Author by

Barış Velioğlu

Updated on June 23, 2022

Comments

  • Barış Velioğlu
    Barış Velioğlu almost 2 years

    I want an ASP:NET WebForms Repeater control to put an index next to each of its output rows automatically. How can I do that?

    Example:

       Name
    1  John
    2  Jack
    3  Joe
    
  • Hans Kesting
    Hans Kesting about 12 years
    Maybe use a <%# Container.ItemIndex + 1 %>, to get a 1-based number.
  • Rawling
    Rawling about 12 years
    Cheers, I've put that in. I assume it'll work :) My own use case uses the zero-based index.
  • Marcel
    Marcel over 7 years
    I used DataBinder.Eval(Container, "ItemIndex")
  • user123456
    user123456 over 3 years
    how to force repeater generate new itemtemplate on specific index @Rawling