ASP.Net:ListView 的 ItemTemplate 中的条件逻辑 [英] ASP.Net: Conditional Logic in a ListView's ItemTemplate

查看:28
本文介绍了ASP.Net:ListView 的 ItemTemplate 中的条件逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据绑定字段是否为空来显示 ItemTemplate 的某些部分.以下面的代码为例:

I want to show certain parts of an ItemTemplate based according to whether a bound field is null. Take for example the following code:

(为简洁起见,LayoutTemplate 等代码已被移除)

<asp:ListView ID="MusicList" runat="server">
    <ItemTemplate>
        <tr>
            <%
                if (Eval("DownloadLink") != null)
                {
            %>
            <td>
                <a href="<%#Eval("DownloadLink") %>">Link</a>
            </td>
            <%
                } %>
        </tr>
    </ItemTemplate>
</asp:ListView>

上面给出了以下运行时错误:

The above gives the following run-time error:

Eval() 等数据绑定方法,XPath() 和 Bind() 只能使用在数据绑定控件的上下文中.

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

那么如何在 ItemTemplate 中放置一些条件逻辑(如上述)?

So how can put some conditional logic (like the above) in an ItemTemplate ?

推荐答案

如何将控件的可见"属性绑定到您的条件?类似的东西:

What about binding the "Visible" property of a control to your condition? Something like:

<asp:ListView ID="MusicList" runat="server">
   <ItemTemplate>
    <tr runat="server" Visible='<%# Eval("DownloadLink") != null %>'>
        <td>
            <a href='<%#Eval("DownloadLink") %>'>Link</a>
        </td>
    </tr>
   </ItemTemplate>
</asp:ListView>

这篇关于ASP.Net:ListView 的 ItemTemplate 中的条件逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆