Ternary inline ASP.NET in Repeater control
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 开发者_StackOverflow社区in a Repeater control? How?
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 %>
精彩评论