ASP.NET using Bind/Eval in .aspx in If statement(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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ASP.NET 在 If 语句中的 .aspx 中使用 Bind/Eval
基础教程推荐
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
