c# - 如何根据数据绑定(bind)到 ASP.NET 中该行的值在我的转发器中设置表行颜色?

标签 c# html asp.net repeater

我有一个转发器控件:

 <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/

相关文章:

asp.net - 无法加载文件或程序集...数组下标超出范围

c# - 在 UWP 中连接到数据库时无法加载文件或程序集

c# - 右对齐 TextBlock,TextWrapping 在左侧环绕

c# - System.reactive跨线程操作荒谬

java - 使用 Java 将 HTML 文件读取到 DOM 树

c# - Aspose.Cells.CellsException - "You are using an evaluation copy and have opened files exceeding limitation"

c# - 如何以编程方式获取最小化应用程序的屏幕截图?

jquery - 更改 jquery ui 组合框字体

javascript - 使用 Javascript 读取服务器端文件

c# - Microsoft Access 数据库引擎找不到对象“Sheet1$”