ASP.NET在If语句中使用.aspx中的Bind / Eval
在我的.aspx中,我希望根据来自绑定的值添加一个If语句。 我尝试过以下方法:
monkeys!!!!!! (please be aware there will be no monkeys, this is only for humour purposes)
IsLinkable是来自Binder的bool。 我收到以下错误:
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)"; } }
我很确定你可以做类似以下的事情
(注意我没有编译器方便测试确切的语法)
text = '<%# string.Format("{0}", (bool)Eval("IsLinkable") ? "Monkeys!" : string.Empty) %>'
是的,这是c#和您使用的vb.net,因此您需要为三元运算符使用vb语法。
编辑 – 能够投入到简单的数据绑定情境中,就像一个魅力。
您可以使用asp:PlaceHolder
并在Visible中放入eval。 如下所示
monkeys!!!!!! (please be aware there will be no monkeys, this is only for humour purposes)
如果您在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)"; } } }
我知道这个答案在当天有点晚了,但是这里值得我解决的问题是:
<%# (bool)Eval("IsLinkable") ? "monkeys!!!!!!" : "" %>
OMG这花了太长时间才搞清楚……
formula.type是链接表的int列。 感谢您的其他贡献,以获得我的决议。
您可以创建一个方法来评估值并返回所需的值。
<%# IsLinkableABool( Eval("IsLinkable") ) %>
在后面的代码中,您可以创建如下方法
protected String IsLinkableABool(String isLinkable) { if (isLinkable == Boolean.TrueString) { return "monkeys!!!!!! (please be aware..."; } else { return String.Empty; } }
每当我需要处理数据绑定控件中的条件时,我都会使用OnItemDataBound事件。
所以你可以这样做:
protected void DataBound_ItemDataBoundEvent() { bool IsLinkable = (bool)DataBinder.Eval(e.Item.DataItem, "IsLinkable"); if(IsLinkable) { //do stuff } }
我们需要查看其余代码,但错误消息给了我一些提示。 当您在数据绑定控件内时,您只能使用Eval。 诸如转发器,数据网格等等。
如果您不在数据绑定控件之外,则可以将值加载到代码隐藏的变量中并将其公开。 然后你可以在ASPX上使用它进行条件处理。
对于FormView
控件,请参阅此链接 。
这是示例代码。 我的aspx页面FormView
控件如下所示:
我正在检查<%# eval("PreviousDegreeYN") %>
如果我的eval("PreviousDegreeYN") == True
,我想在我的标签“ lblPYN ”中显示Yes
protected void fv_DataBound(object sender, EventArgs e) { FormViewRow row = fv.Row; //Declaring Variable lblPYN Label lblPYN; lblPYN = (Label)row.FindControl("lblPYN"); if (lblPYN.Text == "True") { lblPYN.ForeColor = Color.Blue; lblPYN.Text = "Yes"; } else { lblPYN.ForeColor = Color.Blue; lblPYN.Text = "No"; } }
放置条件aspx页面不是一个好主意。也是凌乱的。 你可以使用三元运算符。但我建议你使用网格视图的rowdatabound事件。 步骤1 – 转到网格视图属性。单击照明按钮以列出所有事件。 第2步 – 在rowdatabound上给出一个名称,然后双击
protected void onrow(对象发送者,GridViewRowEventArgs e)
上述就是C#学习教程:ASP.NET在If语句中使用.aspx中的Bind / Eval分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
{ if (e.Row.RowType == DataControlRowType.DataRow) { TableCell statusCell = e.Row.Cells[8];//Means column 9 if (statusCell.Text == "0") { statusCell.Text = "No Doc uploaded"; } else if (statusCell.Text == "1") { statusCell.Text = "Pending"; } else if (statusCell.Text == "2") { statusCell.Text = "Verified"; } } }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1030609.html