Inline IF statement for ItemIndex asp:Repeater VB.NET

10,516

Have you tried something like:

<ItemTemplate> 
           <div class='tab<%# IIf ( Container.ItemIndex = 0, "highlight", "")%> '>
               'Other stuff here 
            </div> 
</ItemTemplate>
Share:
10,516
jaywon
Author by

jaywon

I like code!

Updated on June 05, 2022

Comments

  • jaywon
    jaywon about 2 years

    I am trying to do an inline IF statement inside a asp:Repeater control to add a class to the first item, but I can't quite seem to figure it out.

    Basically the code I have right now that is not working but should give an idea of what I'm "trying" to do looks like this.

       <asp:Repeater ID="rptrTabRepeater" runat="server">
           <ItemTemplate>
               <div class="tab <%= If Container.ItemIndex = 0 Then %>highlight<% End If%>">
                   'Other stuff here
                </div>
           </ItemTemplate>
       </asp:Repeater>
    

    I have tried using the OnItemDataBound event but the delegate interface cannot return a value. If I'm going to do anything from a code-behind function really it would just need to be an "echo" kind of function which I wasn't quite sure how to get the item index in a code behind function. If I could do something inline like my example that would be the best solution for me.

    Any better solutions welcome as well. Thanks!

    EDIT: The compile error I am getting is:

        Compiler Error Message: BC30201: Expression expected.