Search code examples
asp.netvb.netrepeater

Asp.net Repeater row count


Can anyone tell me how to do a running count in a repeater? I've made this code that works but the variable (RC) doesn't seem to be counting up. Here's the code:

<asp:Repeater ID="FPRepeater" runat="server" DataSourceID="FPDataSource">

           <ItemTemplate>
               <%Dim RC As Int32
                   Dim divc As String%>
                   <%If (RC + 1 Mod 1 = 0) Then
                           divc = "one-third mobile-collapse"
                       ElseIf (RC + 1 Mod 2 = 0) Then
                           divc = "one-third one-third-second mobile-collapse"
                       ElseIf (RC + 1 Mod 3 = 0) Then
                           divc = "one-third one-third-last mobile-collapse"
                       End If%>

                    <div class="<%=divc %>">
        <img src="/images/<%# Eval("ImName")%>" alt="<%# Eval("Heading")%>" />      
                            <h2><%# Eval("Heading")%></h2>                   
                           <p><%# System.Web.HttpUtility.HtmlDecode(Eval("ContText"))%></p>                   
                        </div>
               <%RC = (RC + 1)%>
                </ItemTemplate>
                </asp:Repeater><!--/info-box-->

<asp:SqlDataSource ID="FPDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:Conn %>" SelectCommand="SELECT * FROM [tblContent] WHERE (Location = 'Home') AND (Deleted = 'False') Order By OrderBy ASC">

What i'd thought would happen is that <%RC = (RC + 1)%> would automatically add 1 to RC every time the row repeated.

All it currently does is pull back 1, 1, 1 instead of 1, 2, 3. Where am i going wrong? Thanks


Solution

  • Just put down below lines above your repeater control

    <script runat="server">
            Dim RC As Int32 = 1
    </script>
    

    As well as remove Dim RC As Int32 from ItemTemplate