asp.net-在listview中突出显示单元格

时间:2015-07-10 11:23:11

标签: asp.net listview conditional-statements

我有这段代码可以正常工作并突出显示标签。但是,我希望它突出整个单元格而不仅仅是标签。

任何帮助将不胜感激!

protected void HighLight_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        Label TotalTimeLabel = (Label)e.Item.FindControl("TotalTime");
        if (TotalTimeLabel != null)
        {
            Decimal Total;
            if (Decimal.TryParse(TotalTimeLabel.Text, out Total) == true)
            {
                if (Total > (Decimal)1.5)
                {
                    TotalTimeLabel.BackColor = System.Drawing.Color.Red;
                    TotalTimeLabel.ForeColor = System.Drawing.Color.Black;
                }
            }
        }
    }
}

该表的代码如下

<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="HighLight_ItemDataBound" >
        <LayoutTemplate>
             <table cellpadding="1" class="TableCSS" runat="server" id="tblProducts">
              <tr runat="server" style="background-color:lightgrey">
               <th runat="server">enNotificationNoNI</th>
               <th runat="server">TotalTime</th>
               <th runat="server">TPTIMEIN</th>
               <th runat="server">Status</th>
              </tr>
              <tr runat="server" id="itemPlaceholder" />
            </table>
        </LayoutTemplate>
        <ItemTemplate>
            <tr runat="server" class="TableData">
             <td>
              <asp:Label ID="enNotificationNoNI" runat="Server" Text='<%#Eval("enNotificationNoNI") %>' />
             </td>
             <td>
              <asp:Label ID="TotalTime" runat="Server" Text='<%#Eval("TotalTime") %>' />
             </td>
             <td>
              <asp:Label ID="TPTIMEIN" runat="Server" Text='<%#Eval("TPTIMEIN") %>' />
             </td>
             <td>
              <asp:Label ID="Status" runat="Server" Text='<%#Eval("Status") %>' />
             </td>
            </tr>
        </ItemTemplate>
    </asp:ListView>

1 个答案:

答案 0 :(得分:1)

试试此代码

整行

Control ctrl = TotalTimeLabel.Parent.Parent;
HtmlTableRow tblrw = (HtmlTableRow)ctrl;
tblrw.BgColor = System.Drawing.Color.Red.Name;

对于一个单元格

Control ctrl = TotalTimeLabel.Parent;
HtmlTableCell tblcl = (HtmlTableCell)ctrl;
tblcl.BgColor = System.Drawing.Color.Red.Name;

让我知道这是否有效:)