Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i do an if statement inside a repeater

<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....

like image 936
Monsters Avatar asked Aug 21 '09 19:08

Monsters


4 Answers

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>
like image 159
Gavin H Avatar answered Sep 20 '22 13:09

Gavin H


You could try creating a kind of ViewModel class, do the decision on your code-behind, and then be happy with your repeater, simply displaying the data that it is being given.

This is a way to separate logic from UI. You can then have a dumb-UI that simply displays data, without having to decide on what/how to show.

like image 28
Bruno Reis Avatar answered Sep 21 '22 13:09

Bruno Reis


You could do this with user controls:

<ItemTemplate>
    <uc:Content1 runat='server' id='content1' visible='<%# Container.DataItem("property") == "test" %>'/>
    <uc:Content2 runat='server' id='content2' visible='<%# Container.DataItem("property") != "test" %>'/>
</ItemTemplate>
like image 34
Keltex Avatar answered Sep 21 '22 13:09

Keltex


Looks like I got this mixed up with the actual databinding

You can do it like so:

<asp:Repeater runat="server"> 
    <ItemTemplate>    
        <% if (((Product)Container.DataItem).Enabled) { %>
        buy it now!
        <% } else {%>
        come back later!
        <% } %>
    </ItemTemplate>
</asp:Repeater>

like image 45
Brian Surowiec Avatar answered Sep 19 '22 13:09

Brian Surowiec