我有一个转发器控件:
<table style="width: 100%">
<asp:Repeater runat="server" ID="rptOptions" OnItemDataBound="rptOptions_ItemDataBound">
<HeaderTemplate>
<tr>
<td class="GridHeader">Account</td>
<td class="GridHeader">Margin</td>
<td class="GridHeader">Symbol</td>
<td class="GridHeader">Price</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class="GridRow"><asp:Label runat="server" ID="lblOptionAccount"></asp:Label></td>
<td class="GridRow"><asp:Label runat="server" ID="lblOptionMargin"></asp:Label></td>
<td class="GridRow"><asp:Label runat="server" ID="lblOptionSymbol"></asp:Label></td>
<td class="GridRow"><asp:Label runat="server" ID="lblOptionPrice"></asp:Label></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
以及以下代码隐藏数据绑定(bind)方法:
protected void rptOptions_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Option rowOption = (Option)e.Item.DataItem;
((Label)e.Item.FindControl("lblOptionAccount")).Text = rowOption.Account;
((Label)e.Item.FindControl("lblOptionMargin")).Text = rowOption.Margin ? "Y" : "N";
((Label)e.Item.FindControl("lblOptionSymbol")).Text = rowOption.Symbol;
((Label)e.Item.FindControl("lblOptionPrice")).Text = rowOption.Price.ToString("C", currencyFormat);
}
}
该网格中有更多列,但我已将其精简以解决问题。
现在,我想做的是根据价格金额更改 tr 的背景颜色。如果它在不同的级别内,我想相应地改变行的背景颜色。
我是否必须使用 javascript 执行此操作,或者有什么方法可以访问代码隐藏中的表行以设置此颜色?
最佳答案
让它运行到 runat="Server"
<tr runat="server" ID="trHeader"></tr>
然后在数据绑定(bind)事件后面的代码中按 ID 找到表格行,就像您正在为其他服务器端控制和更改颜色所做的那样。
关于c# - 如何根据数据绑定(bind)到 ASP.NET 中该行的值在我的转发器中设置表行颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3119062/