如果语句在转发器中使用ItemTemplate

我正在使用ASP.NET Repeater来显示

的内容。 它看起来像这样:

 
Some data

它工作正常,但我想在ItemTemplate有一个if()语句,所以我可以有条件地确定我是否要打印出一个

标签。

所以我想要这样的东西:

 
Some data

有什么办法可以实现这个目标吗?

PS。 CurrentItemCount刚刚组成。 我还需要一种方法来获取if()语句中的当前项目计数。 但我似乎只能从获得它 ,不能与if()语句一起使用?

我会使用codebehind:

 protected void OnCheckboxListItemBound(Object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { HtmlTableRow itemRow = (HtmlTableRow) e.Item.FindControl("itemRow"); itemRow.Visible = e.Item.ItemIndex % 2 == 0; } } 

另一种方法(如果性能不是问题):

           

如果你正在尝试制作一个2列表,这可以解决问题

 <%# Container.ItemIndex % 2 == 0 ? "" : "" %>  Some data  <%# Container.ItemIndex % 2 != 0 ? " : "" %> 

改变了一些事情:所有行的id="itemRow"会导致重复的id不允许。

删除了runat="server"因为在这种情况下没有意义。

我有两个示例,对于示例,我将转发器绑定到一个字符串数组(仅用于演示目的)

 void BindCheckboxList() { checkboxList.DataSource = new string[] { "RowA", "RowB", "RowC", "RowD", "RowE", "RowF", "RowG" }; checkboxList.DataBind(); } 

示例1:在de codebehind中创建一个方法,然后将绑定元素投射回来评估您想要的值。

在CodeBehind中创建Methode(示例1):

 protected string StringDataEndsWith(object dataElement, string endsWith, string returnValue) { // for now an object of the type string, can be anything. string elem = dataElement as string; if (elem.EndsWith(endsWith)) { return returnValue; } else { return ""; } } 

在.aspx文件中(示例1):

   
<%# StringDataEndsWith(Container.DataItem,"A","") %> <%# Container.DataItem %> <%# StringDataEndsWith(Container.DataItem,"G","") %>

示例2:您可以在.aspx文件中使用直接强制转换

DirectCast示例(后面没有代码):

   
<%# Convert.ToString(Container.DataItem).EndsWith("A") ? "" : "" %> <%# Container.DataItem %> <%# Convert.ToString(Container.DataItem).EndsWith("G") ? "" : "" %>

我希望这就是你要找的东西。 问候。

如果您想要对其他项目执行某些操作,请使用交替项目模板。 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.alternatingitemtemplate.aspx