<asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1" onitemdatabound="Repeater1_ItemDataBound"> <ItemTemplate> <asp:Label ID="LabelName" runat="server" Text='<%#Eval("NickName") %>' /> </ItemTemplate> </asp:Repeater>
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { DataRowView row = e.Item.DataItem as DataRowView; if (row["NickName"].ToString().Length > 4) { Label label = e.Item.FindControl("LabelName") as Label; label.ForeColor = Color.Red; } } }
服务端控件数据绑定时属性必须用单引号''
<asp:Label ID="LabelName" runat="server" Text='<%#Eval("NickName") %>' />
还可以参考
http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.repeater.onitemdatabound.aspx