Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline script conditional statement inside a ListView

Tags:

c#

asp.net

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>
like image 303
esvendsen Avatar asked Mar 11 '26 14:03

esvendsen


1 Answers

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" />
<% } %>
like image 59
Oded Avatar answered Mar 14 '26 02:03

Oded



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!