How can a add a row counter to a Repeater?

S

Stephen Miller

What is the best method to add a row counter to a Repeater control? If
for example, I assigned a DataView to a Repeater in the code behind
like:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Repeater1.DataSource = CreateDataSource()
Repeater1.DataBind()
End Sub

Function CreateDataSource() As ICollection
Dim dt As New DataTable()
Dim dr As DataRow
dt.Columns.Add(New DataColumn("myValue", GetType(String)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(1) = "Item " + i.ToString()
dt.Rows.Add(dr)
Next i
Dim dv As New DataView(dt)
Return dv
End Function

I was hoping to declare an inline variable and increment for each
iteration of the ItemTemplate, ie:

<%
Dim lngRow As Long = -1
%>
<asp:Repeater id="Repeater1" runat="server">
<ItemTemplate>
<%
lngRow += 1
%>
<%# lngRow & "-" & DataBinder.Eval(Container.DataItem, "myValue")
%><br>
</ItemTemplate>
</asp:Repeater>

.... but, it doesn't consider lngRow to be a global variable and errors
when I attempt to increment.

Without altering the DataSource (and yes, I know my sample code
already has a counter), what is the best way to add a counter?

Thanks,

Stephen
 
?

=?iso-8859-1?Q?S=F8ren_Lund_Jensen?=

Stephen said:
What is the best method to add a row counter to a Repeater control? If
for example, I assigned a DataView to a Repeater in the code behind
like:

I usually declare a counter in my code behind and increment it in the
OnDataBound event where I also bind it to a control in the repeater.
Alternatively you could create a method which returns the counter, you
then call this method from your ASPX file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top