如何在Databound ListView中创建If语句

时间:2011-04-04 09:48:42

标签: asp.net vb.net

我有一个包含许多高级控件和html标签的ListView。当集合中的第一个配置文件是当前配置文件时,ListView绑定到配置文件的集合。当前配置文件与其他配置文件的差异很小,即flash嵌入,js和其他一些东西。我可以访问我的ListView Container.DataIndex属性,它给出了0作为索引中的第一项,但我无法使用内联If语句这样

<% If Container.DataIndex = 0 Then %>
do stuff
<% EndIf %>

这是因为我必须放置pound来访问数据绑定项,但这不是

<%# If Container.DataIndex = 0 Then %>
do stuff
<% EndIf %>

如何制作内联If?

2 个答案:

答案 0 :(得分:0)

您的意思是IIF吗? IIF - 返回两个对象中的一个,具体取决于表达式的评估。

答案 1 :(得分:0)

试试这个:

<% if (DataBinder.Eval(Container, "DataItemIndex")) { %>
  do stuff
<% } else { %>
  do other stuff
<%} %>

以下是内联aspx标记的小摘要: http://naspinski.net/post/inline-aspnet-tags-sorting-them-all-out-%283c25242c-3c253d2c-3c252c-3c252c-etc%29.aspx

但我建议使用ListView.ItemDataBound。它在代码隐藏中不易出错且更具可读性。