ASP.NET Repeater Gets the Current Index, Pointer, or Counter

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 elements, and I would like one of the repeaters columns (it repeats and the html table) to be something like ...

Point 1 | some information

Point 2 | some information

... etc.

1 and 2 are the counter.

+68
c # repeater
Jul 01 '09 at 7:22
source share
2 answers

To display the position 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> 
+134
Jul 01 '09 at 7:29
source share

Add a label control to your ItemTemplate repeater . Handle the 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 for:

  protected void RepeaterItemCreated(object sender, RepeaterItemEventArgs e) { Label l = e.Item.FindControl("lblSr") as Label; if (l != null) l.Text = e.Item.ItemIndex + 1+""; } 
+5
Jul 01 '09 at 7:44
source share



All Articles