Repeater 控件中的 If 语句

If statement inside Repeater control

虽然有几个关于这个主题的问题,但我还没有找到满意的答案。

我有一个Repeater,需要显示复杂的内容。模板中需要 IF 语句。我无法将其移至代码隐藏,因为我需要在转发器中注册服务器控件和用户控件。这是我需要的:

<asp:Repeater ID="rCom" runat="server" ClientIDMode="Static">
    <ItemTemplate>
       <%# If CBool(Eval("IsFix")) Then%>
           <%-- HTML content including server and user controls --%>
       <%Else%>
           <%-- HTML content including server and user controls --%>
       <%End If%>
    </ItemTemplate>
</asp:Repeater>

以上抛出一个编译器错误。关于如何实现这一目标的任何想法?我需要评估 If 语句中的 IsFix 字段。

从具有 if 语句的代码块中删除 #:-

<asp:Repeater ID="rCom" runat="server" ClientIDMode="Static">
    <ItemTemplate>
       <% CBool(Eval("IsFix")) Then%>
           <%-- HTML content including server and user controls --%>
       <%Else%>
           <%-- HTML content including server and user controls --%>
       <%End If%>
    </ItemTemplate>
</asp:Repeater>

编辑:

尝试使用条件运算符(我不确定VB.NET中的语法,请检查):-

<%# If(CBool(Eval("IsFix")), "Do Something", "Else do something" %>

我会将每组内容放在一个服务器端面板中,然后在 ItemDataBound 事件中,将一个面板的可见性设置为 true,另一个面板设置为 false。 如果在服务器端设置了可见性,那么前端内容甚至都不会呈现。

完美运行!!

<%# If(CBool(Eval("bitExtemporaneo")) = True, "<img src=""Imagenes/Extempo.png"" alt=""Extemporaneo""/>", "")%>

从有 if 语句的代码中删除 #。

然后像这样使用代码。

<%
                    object objIsFix = ((System.Data.DataTable)rep.DataSource).Rows[_nIndex]["IsFix"];
                if (bool(objIsFix)) {%>
                <%-- HTML content including server and user controls --%>
                <%}Else{%>
                <%-- HTML content including server and user controls --%>
                <%}%>

'_nIndex'在cs文件中定义