views:

574

answers:

4

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

+4  A: 

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.

Bruno Reis
I think this is the crux of it, have the data totally clean before using a repeater. Life would be nice if you could use basic comparators in the repeater though.
Monsters
+1  A: 

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>
Keltex
Yeah, i thought of doing that, it would work, but a bit of a workaround. I've decided that repeater just isnt up for the task, and gone for a for/next loop, works a treat.
Monsters
+3  A: 
Brian Surowiec
Container.dataitem only exists in the.. variable? markup, e.g. <%#
Monsters
You're right, I thought I remembered doing something like this and now that I'm looking at my code it was logic on non-databinded values.
Brian Surowiec
+1  A: 

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>
ghills
This is a pretty good solution actually.
Monsters