How can i do an if statement inside a repeater

Monsters picture Monsters · Aug 21, 2009 · Viewed 22.9k times · Source

<asp:Repeater> is driving me mad..

I need to do

<ItemTemplate>
    <% if (Container.DataItem("property") == "test") {%>
        I show this HTML
    <% } else { %>
        I show this other HTML
    <% } %>
</ItemTemplate>

But I can't for the life of me find any way to make that happen. Ternary isnt any good, because the amount of HTML is quite large, setting labels via a DataBind event is not very good either, as I'd have to have large blocks of HTML in the code-behind.

Surely there's a way to do this....

Answer

Gavin H picture Gavin H · Aug 21, 2009

You could use server side visibility:

<ItemTemplate>
    <div runat="server" visible='<% (Container.DataItem("property") == "test") %>'>
        I show this HTML
    </div>
    <div runat="server" visible='<% (Container.DataItem("property") != "test") %>'>
        I show this other HTML
    </div>
</ItemTemplate>