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
基础教程推荐
- C# 从 List<List<int>> 中删除重 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
