Inline script conditional statement inside ListView

I am trying to display an image inside a ListView control based on the value of the databound property. I tried two methods to do this (one at a time), and both returned "Server tag badly formed" errors. 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>
+3
source share
1 answer

Method 1 :

Instead of using an "attribute value, visibleuse ':

<img src="media-play-button.png" alt="Play" class="mediaplay noborder" 
    runat="server" visible='<%# Eval("MediaType").ToString() == "video" %>' />

Using "leads to the end of the line after <%# Eval(.

Method 2 :

Do not use binding expressions ( <%#%>) for coding blocks ( <%%>):

<% if (((MediaLink)Container.DataItem).MediaType == "video") { %>
<img src="media-play-button.png" alt="Play" class="mediaplay noborder" />
<% } %>
+7

Source: https://habr.com/ru/post/1780448/


All Articles