本文介绍了ASP.NET使用绑定/评估和演示在if语句的.aspx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的.aspx我期待的基础上,从绑定来的值在if语句中添加。我曾尝试以下内容:

 <%,如果(bool.Parse(EVAL(IsLinkable)作为字符串)){ %GT; 
猴子!!!!!!
(请注意就不会有猴子,
这仅仅是幽默的目的)
<%}%GT;



IsLinkable是从活页夹来一个布尔值。我收到以下错误:

 出现InvalidOperationException 
数据绑定方法如的eval(),XPath的()和绑定( )只能在数据绑定控件的上下文中使用


解决方案

您需要将您的逻辑添加到的ItemDataBound ListView控件的事件。在ASPX你不能在DataBinder的上下文中的if语句:<%#如果()%> 不起作用



看看这里:的



事件将被上调将被绑定到你的ListView,因此在事件的背景是关系到项目的每个项目。



为例,看看你是否可以调整您的情况:

 保护无效ListView_ItemDataBound(对象发件人,ListViewItemEventArgs E)
{
如果(E .Item.ItemType == ListViewItemType.DataItem)
{
标签monkeyLabel =(标签)e.Item.FindControl(monkeyLabel);
布尔可链接=(布尔)的DataBinder.Eval(e.Item.DataItemIsLinkable);
如果(可交联)
monkeyLabel.Text =猴子!!!!!!(请注意将没有猴子,这仅是为了幽默目的);
}
}


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 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.
解决方案

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.

Have a look here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemdatabound.aspx

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 11:58