【发布时间】:2014-04-29 19:06:11
【问题描述】:
我有一个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>
在代码后面我需要为这个<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");
}
}
【问题讨论】:
-
如果当前项是第一项或者数据源只有一项,需要设置吗?
-
如何将数据绑定到中继器?
-
@TroyCarlson 仅当 datasource.count 为 1 时
-
@Icarus rptID.DataSource = myDataTable; rptID.DataBind();