在我的.aspx中,我希望基于来自绑定的值添加If语句。我尝试了以下方法:
<% if(bool.Parse(Eval("IsLinkable") as string)){ %>
monkeys!!!!!!
(please be aware there will be no monkeys,
this is only for humour purposes)
<%} %>
IsLinkable是来自活页夹的布尔。我收到以下错误:
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() %>
不起作用。
在这里看看:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemdatabound.aspx
将绑定到ListView的每个项目都会引发该事件,因此事件中的上下文与该项目相关。
示例,是否可以根据情况进行调整:
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)";
}
}
关于FormView
控制,请参考this link。
放置条件aspx页面不是一个好主意。也很混乱。U可以使用三元运算符。但是我建议您使用网格视图的rowdatabound事件。步骤1:转到网格视图属性。单击照明按钮以列出所有事件。步骤2-在rowdatabound上给一个名称,然后双击
我很确定您可以执行以下操作
((请注意,我没有方便的编译器来测试确切的语法)
text = '<%# string.Format("{0}", (bool)Eval("IsLinkable") ? "Monkeys!" : string.Empty) %>'
是的,这是c#,并且您正在使用vb.net,因此您需要对三元运算符使用vb语法。
编辑-能够陷入一个简单的数据绑定情况,就像一种魅力。
您可以使用asp:PlaceHolder
,并且在Visible中可以放置eval。如下所示
<asp:PlaceHolder ID="plc" runat="server" Visible='<%# Eval("IsLinkable")%>'>
monkeys!!!!!!
(please be aware there will be no monkeys, this is only for humour purposes)
</asp:PlaceHolder>
OMG这花了很长时间才弄清楚...
<asp:PlaceHolder runat="server" Visible='<%# Eval("formula.type").ToString()=="0" %>'>
Content
</asp:PlaceHolder>
formula.type是链接表的int列。感谢您做出的其他贡献。
如果您在获取Bazzz答案中的e.Item.DataItem
时遇到问题,请尝试>]
protected void ListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
using (ListViewDataItem listViewDataItem = (ListViewDataItem) e.Item)
{
if (listViewDataItem != null)
{
Label monkeyLabel = (Label)e.Item.FindControl("monkeyLabel");
bool linkable = (bool)DataBinder.Eval(listViewDataItem , "IsLinkable");
if (linkable)
monkeyLabel.Text = "monkeys!!!!!! (please be aware there will be no monkeys, this is only for humour purposes)";
}
}
}
我知道这个答案有点迟了,但是值得在这里解决我的问题:
您可以创建一种方法来评估值并返回所需的值。
无论何时需要处理数据绑定控件中的条件,我都会使用OnItemDataBound事件。
我们需要查看您的其余代码,但是错误消息给了我一些提示。只有在数据绑定控件中时,才能使用Eval。诸如中继器,数据网格等之类的东西。