如何根据ASP.NET中该行的值数据绑定在我的转发器中设置表格行颜色?

Mr.*_*ahh 3 html c# 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>
Run Code Online (Sandbox Code Playgroud)

以下代码隐藏数据绑定方法:

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);

            }
        }
Run Code Online (Sandbox Code Playgroud)

该网格中有更多列,但我只是针对这个问题进行了细化.

现在,我想要做的是根据价格金额更改tr的背景颜色.如果它在不同的级别,我想相应地改变行背景颜色.

我是否必须使用javascript执行此操作,或者是否有某些方法可以访问代码隐藏中的表行以设置此颜色?

Muh*_*tar 5

使它成为runat ="Server"

<tr runat="server" ID="trHeader"></tr>
Run Code Online (Sandbox Code Playgroud)

然后在数据绑定事件后面的代码中找到ID表中的ID,就像你正在为其他服务器端控件做的那样并改变颜色.


dav*_*ben 5

另外一个选项:

<tr class="<%# GetClassForPrice( Container.DataItem ) %>">
Run Code Online (Sandbox Code Playgroud)

并在代码隐藏中

protected string GetClassForPrice( object data ) 
{
    var rowOption = (Option)data;
    if(rowOption.Price > 100) return "red";
    else return "green";
}
Run Code Online (Sandbox Code Playgroud)

还有...你不使用数据绑定的任何原因?它会让你消除你的 ItemDataBound 代码隐藏方法。

<tr>
    <td class="GridRow"><%# Eval("Account") %></td>
    <td class="GridRow"><%# ((bool)Eval("Margin")) ? "Y" : "N" %></td>
    <td class="GridRow"><%# Eval("Symbol") %></td>
    <td class="GridRow"><%# Eval("Price", "{0:c}") %></td>
</tr>
Run Code Online (Sandbox Code Playgroud)