我可以在GridView ItemTemplate中使用IF语句? [英] Can I use an IF statement in a GridView ItemTemplate?
问题描述
我有一个简单的GridView ItemTemplate中,看起来像这样:
I have a simple gridview ItemTemplate that looks like this:
<asp:TemplateField HeaderText="User">
<ItemTemplate>
<a href="mailto:<%# Eval("Email") %>"><%# Eval("Name") %></a>
</ItemTemplate>
</asp:TemplateField>
然而,并不是所有该列表上的用户的已存储在系统中,这意味着评估和演示电子邮件( 电子邮件)有时会返回空白。发生这种情况时,我宁愿没有在球场上的链接,因为邮寄地址不会在未得到一个电子邮件地址工作。
However, not all of the users on this list have emails stored in the system, which means Eval("Email") sometimes returns blank. When this happens, I'd rather not have a link on the field, since the mailto won't work without an email address.
我怎样才能做到这一点?我希望我可以用在演示代码中的IF语句有点像传统的ASP如何使用工作。如果不是这样,我想我可以创建一个包含整个HREF HTML ...
How can I do this? I was hoping I could use an IF statement in the presentation code kind of like how classic ASP used to work. If not, I suppose I could create a property on my datasource that includes the entire HREF html...
推荐答案
在我的数据源的属性评估
你可以使用任何给定的公共职能。所以,你可以尝试做一些这样的:
Instead of Eval
you can use any given public function. So you might try and do something like the following:
<ItemTemplate>
<%# (String.IsNullOrEmpty(Eval("Email").ToString()) ? String.Empty : String.Format("<a href='mailto:{0}'>{1}</a>", Eval("Email"), Eval("Name")) %>
</ItemTemplate>
如果还没有尝试过确切的语法,但我使用类似的东西在我的网页之一。
If have not tried the exact syntax, but I'm using something like that in one of my pages.
这篇关于我可以在GridView ItemTemplate中使用IF语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!