ListView中的内联脚本条件语句

esv*_*sen 3 c# asp.net

我正在尝试根据数据绑定属性的值在ListView控件中显示图像.我已经尝试了两种方法(一次一个),并且都返回了"服务器标签格式不正确"的错误.请考虑以下代码.

<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>
Run Code Online (Sandbox Code Playgroud)

Ode*_*ded 7

方法1:

而不是使用"visible属性值,使用':

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

使用"导致字符串在之后终止<%# Eval(.

方法2:

不要使用绑定表达式(<%#%>)来编码块(<%%>):

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