您必须使用中继器的 ItemDataBound 方法。
编辑:
当您想更改 的类时<td>
,您必须为其添加属性runat="server"
。您的标记可能如下所示:
<asp:Repeater ID="rptMyRepeater" runat="server" OnItemDataBound="rptMyRepeater_ItemDataBound">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem, "Won") %> </td>
<td><%# DataBinder.Eval(Container.DataItem, "Lost") %> </td>
<td runat ="server" id="tdResult"><%# DataBinder.Eval(Container.DataItem, "Result") %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
和代码:
protected void rptMyRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
HtmlTableCell tdResult = e.Item.FindControl("tdResult") as HtmlTableCell;
if (DataBinder.Eval(e.Item.DataItem, "Result").ToString() == "1")
{
tdResult.Attributes["class"] = "Won";
}
else if (DataBinder.Eval(e.Item.DataItem, "Result").ToString() == "2")
{
tdResult.Attributes["class"] = "Lost";
}
}
}