Ternary inline ASP.NET in Repeater control

人盡茶涼 提交于 2019-12-11 05:40:05

问题


Imagine that I have a link button called "readMore" in the ItemTemplate of a repeater, and I want to set display: none; for it, when the content of each post is less than say, 2000 characters.

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

In PHP, you can simply write something like:

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

However, I tested this code and it trowed and error:

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

Is it possible to write ternary inline ASP.NET in a Repeater control? How?


回答1:


It is not an issue of ternary operator; it is an issue of Databound controls because you have to use # instead of =.

Use this

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

Instead of

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


来源:https://stackoverflow.com/questions/7009834/ternary-inline-asp-net-in-repeater-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!