if语句EVAL吗? [英] eval in if statement?
问题描述
<% if(Eval("SaveDate") != DBNull.Value){ %>
do magic
<%} %>
给我的错误:数据绑定方法如的eval(),XPath的()和bind()只能在数据绑定控件的上下文中使用
gives me error: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
我可以这样写:&LT;%#的eval(SaveDate)= DBNull.Value!?做魔术
但我需要做大量的HTML魔法if语句。
I could write : <%# Eval("SaveDate") != DBNull.Value ? do magic But I need to do lots of html magic in if statement.
我知道我应该以使用eval添加#,但不知道正确的语法。
I know I should add # in order to use Eval, but not sure about correct syntax.
推荐答案
一个解决办法是包装在=服务器标签内容有可见的价值,如,
One solution is to wrap the content in a runat="server" tag with a Visible value, e.g.,
<div runat="server" Visible='<%# Eval("SaveDate") != DBNull.Value %>'>
do magic
</div>
DIV
可以是任意的HTML标签,但&LT; ASP:面板&gt;
和 &所述; ASP:占位符&GT;
也可以使用。请注意,使用魔法仍然是数据绑定,因此,如果它包含昂贵code或code可能会产生错误它不是一个完美的解决方案,如果评估(SaveDate)==的DBNull .value的
。
div
can be any HTML tag, but <asp:Panel>
and <asp:PlaceHolder>
could also be used. Note that "do magic" is still databound, so it's not a perfect solution if it contains expensive code or code that could generate an error if Eval("SaveDate") == DBNull.Value
.
注意可见=假
会忽略标记,并从生成的HTML的所有内容,这意味着它是非常不同的风格=显示:无
或风格=可见:隐藏。
,所以不用担心。
Note that Visible="false"
will omit the tag and all its contents from the generated HTML, this means that it is very different from style="display:none"
or style="visible:hidden"
, so don't worry about that.
但是,如果你的做魔术的是的相当复杂的,另一种比较简单的解决方案(一个黑客位)的是:使用中继器(或FormView控件),其数据源设置为数组一个项目(可见)或任何项目(隐藏):
But, if your "do magic" is reasonably complex, another rather simple solution (a bit of a hack) is: use a Repeater (or FormView) with its DataSource set to an array of one item (visible) or no items (hidden):
<asp:Repeater runat="server" DataSource='<%# ElementIfTrue(Eval("SaveDate") != DBNull.Value) %>'
<ItemTemplate>
do magic
</ItemTemplate>
</asp:Repeater>
protected IEnumerable ElementIfTrue(bool condition)
{
if (condition)
return new object[] { Page.GetDataItem() };
else
return new object[0];
}
数据源阵列的实际内容是空的(隐藏)或元素你已经绑定。这可以确保您仍然可以调用&LT;%#的eval(...)%方式&gt;
ItemTemplate模板内
The actual contents of the datasource array is either empty (hidden) or the element you were already binding to. This makes sure you can still call <%# Eval(...) %>
inside the ItemTemplate.
通过这种方法,你的魔做是,如果数据源有一个或多个项目将只执行一个模板。这是由 ElementIfTrue
照顾。这是一个有点脑筋急转弯的,但它可以在一段时间一次为您节省每一个。
With this approach, your "do magic" is a template which will only be executed if DataSource has one or more items. Which is taken care of by ElementIfTrue
. It's a bit of a mind bender, but it can save you every once in a while.
作为一个方面说明:包装你的使用魔法在用户控件还可以保留下来的复杂性。你并不真的需要改变的事情在你的HTML / ASP.NET标签组合(&LT;%#的eval(...)%GT;
仍然有效甚至里面的用户控件)。
As a side note: packing your "do magic" in a user control can also keep the complexity down. You don't really need to change a thing in your HTML/ASP.NET tag mix (<%# Eval("...") %>
still works even inside a user control).
这篇关于if语句EVAL吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!