vb.net 中继器控件中的 if 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28090011/
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-09-17 18:42:55  来源:igfitidea点击:

If statement inside Repeater control

asp.netvb.netrepeater

提问by navigator

Although there are several questions on this topic, I haven't found any satisfactory answers.

虽然关于这个话题有几个问题,但我还没有找到任何令人满意的答案。

I have a Repeater that needs to display complex content. An IFstatement is required within the template. I can't move this to the code-behind as I need to have server controls and user controls registered within the repeater. Here is what I need:

我有一个需要显示复杂内容的中继器。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>

The above throws a compiler error. Any idea on how to achieve this? I need to evaluate the IsFixfield in the Ifstatement.

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

回答by Jeremy

I would put each set of content within a server side panel, then on ItemDataBound event, set the visibility of one panel to true, and the other to false. If visibility is set server side, then the front end content never even gets rendered.

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

回答by Rahul Singh

Remove #from the code nuggets which is having ifstatement:-

#从有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>

Edit:

编辑:

Try with conditional operator (I am not sure about the syntax in VB.NET, please check):-

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

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

回答by Mauricio Mendoza

It works perfectly!!

它完美地工作!!

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

回答by cjremond

Remove # from the code where is having if statement.

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

Then use the code like this.

然后像这样使用代码。

<%
                    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' is defined in the cs file

'_nIndex' 在 cs 文件中定义