Inline script conditional statement inside a ListView
I'm trying开发者_高级运维 to display an image inside a ListView control based on the value of a databound property. I've tried two methods of doing this (one at a time) and both returned errors of "The server tag is not well formed". Consider the code below.
<ItemTemplate>
<div class="left">
<!-- Method 1 -->
<img src="media-play-button.png" alt="Play" class="mediaplay noborder" runat="server" visible="<%# Eval("MediaType").ToString() == "video" %>" />
<!-- Method 2 -->
<%# if (((MediaLink)Container.DataItem).MediaType == "video") { %>
<img src="media-play-button.png" alt="Play" class="mediaplay noborder" />
<%# } %>
</div>
</ItemTemplate>
Method 1:
Instead of using "
for the visible
attribute value, use '
:
<img src="media-play-button.png" alt="Play" class="mediaplay noborder"
runat="server" visible='<%# Eval("MediaType").ToString() == "video" %>' />
Using "
causes the string to terminate after <%# Eval(
.
Method 2:
Don't use binding expressions (<%#%>
) for coding blocks (<%%>
):
<% if (((MediaLink)Container.DataItem).MediaType == "video") { %>
<img src="media-play-button.png" alt="Play" class="mediaplay noborder" />
<% } %>
精彩评论