ASP.NET 在 If 语句中的 .aspx 中使用 Bind/Eval [英] ASP.NET using Bind/Eval in .aspx in If statement

查看:21
本文介绍了ASP.NET 在 If 语句中的 .aspx 中使用 Bind/Eval的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 .aspx 中,我希望添加一个基于来自绑定的值的 If 语句.我尝试了以下方法:

in my .aspx I'm looking to add in an If statement based on a value coming from the bind. I have tried the following:

<% if(bool.Parse(Eval("IsLinkable") as string)){ %>                    
        monkeys!!!!!!
        (please be aware there will be no monkeys, 
        this is only for humour purposes)
 <%} %>

IsLinkable 是来自 Binder 的 bool.我收到以下错误:

IsLinkable is a bool coming from the Binder. I get the following error:

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

推荐答案

您需要将逻辑添加到 ListView 的 ItemDataBound 事件.在 aspx 中,您不能在 DataBinder 的上下文中使用 if 语句:<%# if() %> 不起作用.

You need to add your logic to the ItemDataBound event of ListView. In the aspx you cannot have an if-statement in the context of a DataBinder: <%# if() %> doesn't work.

看看这里:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemdatabound.aspx

将为绑定到 ListView 的每个项目引发该事件,因此事件中的上下文与该项目相关.

The event will be raised for each item that will be bound to your ListView and therefore the context in the event is related to the item.

例如,看看您是否可以根据您的情况进行调整:

Example, see if you can adjust it to your situation:

protected void ListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        Label monkeyLabel = (Label)e.Item.FindControl("monkeyLabel");
        bool linkable = (bool)DataBinder.Eval(e.Item.DataItem, "IsLinkable");
        if (linkable)
           monkeyLabel.Text = "monkeys!!!!!! (please be aware there will be no monkeys, this is only for humour purposes)";
    }
}

这篇关于ASP.NET 在 If 语句中的 .aspx 中使用 Bind/Eval的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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