How can i do an if statement inside a repeater

2020-07-10 09:13发布

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

6条回答
甜甜的少女心
2楼-- · 2020-07-10 09:28

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>
查看更多
forever°为你锁心
3楼-- · 2020-07-10 09:33

First You Have to Defind a Count variable in your Page.cs file

 <%if (Count == 0)
                         {
                             %>
                    <div style="background-color:#cfe9ed" class="wid_100 left special_text"><%# Eval("CompanyName") %></div>
                       <%}
                         else if (Count == TotalCount - 1)
                         {
                             %>
                        <div style="background-color:#f2f1aa" class="wid_100 left special_text"><%# Eval("CompanyName") %></div>
                        <%}
                         else
                         {
                              %>
                       <div class="wid_100 left special_text"><%# Eval("CompanyName") %></div><% } %>
                       <%Count++;  %>
查看更多
孤傲高冷的网名
4楼-- · 2020-07-10 09:35

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>

查看更多
爷、活的狠高调
5楼-- · 2020-07-10 09:43

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.

查看更多
何必那么认真
6楼-- · 2020-07-10 09:49

I had a similar problem and stumbled across this page. Thanks for the great answers, Gavin and Keltex got me on the right track but I had a bit of a tricky time getting this to work on my page. Ultimately I was able to get it to work with this boolean, so I wanted to share for posterity:

Show Checkbox if false

<asp:CheckBox ID="chk_FollowUp" Visible='<%# (DataBinder.Eval(Container.DataItem, "FollowUp").ToString() == "False") %>' runat="server" />

Show Flag img if true

<asp:Image ID="img_FollowUp" AlternateText="Flagged" ImageUrl="Images/flag.gif" runat="server"
     Visible='<%# DataBinder.Eval(Container.DataItem, "FollowUp") %>' Height="30" Width="30" />
查看更多
我欲成王,谁敢阻挡
7楼-- · 2020-07-10 09:50

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>
查看更多
登录 后发表回答