C# ASP.Net 条件数据绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/653486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.Net conditional databinding
提问by Niels Bosma
<% if(Convert.ToInt32(Eval("NbrOrders"))>=Config.MAX_ENQUIRY_SALES) { %>
...
<% } else { %>
...
<% } %>
Gives me a InvalidOperationException? How do I write conditional html generation in ASP?
给我一个 InvalidOperationException?如何在 ASP 中编写条件 html 生成?
采纳答案by Robin Day
Use an inline statement as John_ states, or, create a function in your code behind that performs the logic required.
使用内联语句作为 John_ 所述,或者在您的代码中创建一个执行所需逻辑的函数。
protected string MyFunction(int nbrOrders)
{
if(nbrOrders>=Config.MAX_ENQUIRY_SALES)
{
return "TrueResult";
}
else
{
return "FalseResult";
}
}
Then use this as follows
然后按如下方式使用
<%# MyFunction(Convert.ToInt32(Eval("NbrOrders"))) %>
EDIT: I've just read a comment on another post that states you want to show different HTML depending on this result. In that case, you can try using the Visible flag of a placeholder containing your code. Such as:
编辑:我刚刚阅读了另一篇文章的评论,该评论指出您希望根据此结果显示不同的 HTML。在这种情况下,您可以尝试使用包含您的代码的占位符的 Visible 标志。如:
<asp:PlaceHolder id="PlaceHolder1" runat="server" visible='<%# (Convert.ToInt32(Eval("NbrOrders"))>=Config.MAX_ENQUIRY_SALES)%>'>
<div>My True Html Here</div>
</asp:PlaceHolder>
<asp:PlaceHolder id="PlaceHolder2" runat="server" visible='<%# !(Convert.ToInt32(Eval("NbrOrders"))>=Config.MAX_ENQUIRY_SALES)%>'>
<div>My FalseHtml Here</div>
</asp:PlaceHolder>
回答by jaloplo
I can't find something wrong in your sentences but comparative you made between Config.MAX_ENQUIRY_SALES and Convert.ToInt32(Eval("NbrOrders")). Are these operator of the same type? Can you show the type of each one in your web page?
我在你的句子中找不到错误,但比较了你在 Config.MAX_ENQUIRY_SALES 和 Convert.ToInt32(Eval("NbrOrders")) 之间所做的比较。这些运算符是同一类型的吗?你能在你的网页中显示每一种的类型吗?
回答by John_
I'm not sure you can add brackets for the conditional binding, the only way I know of doing it is with an inline statement like so:
我不确定您是否可以为条件绑定添加括号,我知道这样做的唯一方法是使用内联语句,如下所示:
<%# Convert.ToInt32(Eval("NbrOrders"))>=Config.MAX_ENQUIRY_SALES) ? Eval("IfTrueValue") : Eval("IfFalseValue") %>
回答by Dustin Campbell
if/else blocks work in ASP .NET as you expect them to. The following works just fine.
if/else 块在 ASP .NET 中如您所期望的那样工作。以下工作得很好。
<% if(DateTime.Now.Second % 2 == 0) { %>
<div>Even</div>
<% } else { %>
<div>Odd</div>
<% } %>
Perhaps the conditional logic in your example is throwing an exception?
也许您示例中的条件逻辑正在引发异常?
回答by Brian Chavez
The problem with @Robin Day's answer is that the following code fails if you have databound children that may or may not have data given the current state of whatever you are rendering. Sometimes it's difficult to maneuver around nullable databound code if you have a complex object graph.
@Robin Day 的答案的问题是,如果您有数据绑定的子级,这些子级可能有也可能没有数据,因为您正在呈现的任何内容的当前状态都将导致以下代码失败。如果您有一个复杂的对象图,有时很难处理可为空的数据绑定代码。
For example, consider:
例如,考虑:
<asp:PlaceHolder runat="server" Visible="<%# VisibleCondition() %>">
<%# ((string)null).ToString("c") %> //an object that may have null data
//given the visible condition
</asp:PlaceHolder>
If VisibleCondition()
returns false
, child controls still get called with DataBind()
which can result in a NullReferenceException
in the example above.
如果VisibleCondition()
返回false
,子控件仍然会被调用,DataBind()
这可能会导致NullReferenceException
上面示例中的 a 。
Here is a better approach, IMHO:
这是一个更好的方法,恕我直言:
public class ConditionalPlaceHolder : PlaceHolder
{
protected override void DataBindChildren()
{
if( this.Visible )
{
base.DataBindChildren();
}
}
}
And used in the following way:
并以下列方式使用:
<web:ConditionalPlaceHolder runat="server" Visible="<%# VisibleCondition1() %>">
//whatever databound code
<%# ((string)notNullGivenVisibleCondition1).ToString() %>
<p>But could be given visible condition 2</p>
</web:ConditionalPlaceHolder>
<web:ConditionalPlaceHolder runat="server" Visible="<%# VisibleCondition2() %>">
//whatever databound code
<%# ((string)notNullGivenVisibleCondition2).ToString() %>
<p>But could be given visible condition 1</p>
</web:ConditionalPlaceHolder>