【问题标题】:Ternary inline ASP.NET in Repeater controlRepeater 控件中的三元内联 ASP.NET
【发布时间】:2011-08-10 11:11:01
【问题描述】:

假设我在转发器的ItemTemplate 中有一个名为“readMore”的链接按钮,并且我想为它设置display: none;,当每个帖子的内容少于2000 个字符时。

<asp:repeater id="postsRepeater" runat="server" 
  onitemdatabound="postsRepeater_ItemDataBound">
    <ItemTemplate>
            <a class="button" href="#" runat='server' id='more'>Read More</a>
    </ItemTemplate>
</asp:repeater>

在 PHP 中,您可以简单地编写如下内容:

<?php echo (contentLength < 2000 ? 'display: none;' : ''); ?>

但是,我测试了这段代码,结果出错了:

<%= Eval("Content").Length < 2000 ? "display: none;" : string.Empty %>

是否可以在 Repeater 控件中编写三元内联 ASP.NET?怎么样?

【问题讨论】:

    标签: asp.net ternary-operator


    【解决方案1】:

    这不是三元运算符的问题;这是数据绑定控件的问题,因为您必须使用# 而不是=

    使用这个

    <%# Eval("Content").ToString().Length < 2000 ? "display: none;" : string.Empty %>
    

    代替

    <%= Eval("Content").ToString().Length < 2000 ? "display: none;" : string.Empty %>
    

    【讨论】:

    • Eval 也返回对象,因此您需要将结果转换或转换为字符串才能获得长度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-17
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 2011-02-04
    • 1970-01-01
    • 2020-03-08
    相关资源
    最近更新 更多