我有一个Repeater
<asp:Repeater runat="server" ID="rptID" OnItemDataBound="repID_ItemDataBound">
<ItemTemplate>
<a href='example.com/somepage.aspx' id="myLink">
<%# Eval("MyVal")%>
</a>
</ItemTemplate>
</asp:Repeater>
在Code Behind中我需要在转发器为1时为此<a>
标记添加一个css类
项目
protected void repID_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
//how to set this class only then count of items is equal with 1
((HtmlGenericControl)e.Item.FindControl("myLink")).Attributes
.Add("class", "Count1");
}
}
答案 0 :(得分:5)
这将为您提供数据源项的计数:
if (((IEnumerable)rptID.DataSource).Cast<object>().Count() == 1)
{
((HtmlGenericControl)e.Item.FindControl("myLink")).Attributes
.Add("class", "Count1");
}
从Calculating Count for IEnumerable (Non Generic)
借用了IEnumerable