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

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

问题描述

我要显示的某些部分的的ItemTemplate 根据绑定字段是否为空为主。就拿下面code:

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

(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()和绑定()只能用
  在数据绑定控件的情况下。

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天全站免登陆