C# 我如何在中继器中执行 if 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1313725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:00:39  来源:igfitidea点击:

How can i do an if statement inside a repeater

c#repeaterif-statement

提问by Monsters

<asp:Repeater>is driving me mad..

<asp:Repeater>快把我逼疯了..

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.

但是我终其一生都找不到任何方法来实现这一目标。三元没有任何好处,因为 HTML 的数量非常大,通过 DataBind 事件设置标签也不是很好,因为我必须在代码隐藏中有大块的 HTML。

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

当然有办法做到这一点......

采纳答案by Bruno Reis

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.

您可以尝试创建一种 ViewModel 类,对您的代码隐藏做出决定,然后对您的转发器感到满意,只需显示它所提供的数据。

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.

这是一种将逻辑与 UI 分开的方法。然后,您可以拥有一个简单的显示数据的哑 UI,而不必决定显示什么/如何显示。

回答by Keltex

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>

回答by Brian Surowiec

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

回答by Gavin H

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>

回答by RJB

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:

我有一个类似的问题,偶然发现了这个页面。感谢您提供出色的答案,Gavin 和 Keltex 使我走上了正确的轨道,但让我在我的页面上使用它时遇到了一些棘手的问题。最终我能够让它与这个布尔值一起工作,所以我想为后代分享:

Show Checkbox if false

如果为假则显示复选框

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

Show Flag img if true

如果为真则显示标志 img

<asp:Image ID="img_FollowUp" AlternateText="Flagged" ImageUrl="Images/flag.gif" runat="server"
     Visible='<%# DataBinder.Eval(Container.DataItem, "FollowUp") %>' Height="30" Width="30" />

回答by Dhaval Patel

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

首先,您必须在 Page.cs 文件中定义一个 Count 变量

 <%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++;  %>