3

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.
jaywon
  • 8,164
  • 10
  • 39
  • 47

1 Answers1

8

Have you tried something like:

<ItemTemplate> 
           <div class='tab<%# IIf ( Container.ItemIndex = 0, "highlight", "")%> '>
               'Other stuff here 
            </div> 
</ItemTemplate>
jaywon
  • 8,164
  • 10
  • 39
  • 47
Ed Schembor
  • 8,090
  • 8
  • 31
  • 37
  • I get "Name 'Container' is not declared." compile error for that? – jaywon Aug 13 '10 at 21:16
  • @Ed, hey you were right, the syntax was just a little off. Needed to be `<%# IIf ( Container.ItemIndex = 0, "highlight", "")%>`. Notice the missing pound sign in your example. If you update your post I'll check you off on the answer. Thanks! – jaywon Aug 20 '10 at 02:31
  • This output `
    ">` in the dom when I tried it :S
    – Simon Arnold Feb 06 '13 at 15:39
  • True, you need to move "tab" inside the output as well, `<%# IIf ( Container.ItemIndex = 0, "tabhighlight", "tab")%>` – lucafik May 19 '14 at 09:27