Fun*_*nky 18 c# if-statement webforms bind eval
在我的.aspx中,我希望根据来自绑定的值添加一个If语句.我尝试过以下方法:
<% if(bool.Parse(Eval("IsLinkable") as string)){ %>
monkeys!!!!!!
(please be aware there will be no monkeys,
this is only for humour purposes)
<%} %>
Run Code Online (Sandbox Code Playgroud)
IsLinkable是来自Binder的bool.我收到以下错误:
InvalidOperationException
Databinding methods such as Eval(), XPath(), and Bind() can only
be used in the context of a databound control.
Run Code Online (Sandbox Code Playgroud)
Baz*_*zzz 19
您需要将您的逻辑添加到ItemDataBoundListView 的事件中.在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)";
}
}
Run Code Online (Sandbox Code Playgroud)
asa*_*yer 15
我很确定你可以做类似以下的事情
(注意我没有编译器方便测试确切的语法)
text = '<%# string.Format("{0}", (bool)Eval("IsLinkable") ? "Monkeys!" : string.Empty) %>'
Run Code Online (Sandbox Code Playgroud)
是的,这是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>
Run Code Online (Sandbox Code Playgroud)
小智 5
OMG,这花了很长时间才弄清楚...
<asp:PlaceHolder runat="server" Visible='<%# Eval("formula.type").ToString()=="0" %>'>
Content
</asp:PlaceHolder>
Formula.Type是链接表的int列。感谢您做出的其他贡献。