0

我正在使用数据列表,而我目前正在处理的这个特定数据列表从数据库中检索数据。它查询数据库中特定 PC 的驱动器,并显示其总空间和可用空间。数据列表中的行应如下所示:

 Drive        Total       Free       Label
 C:/          84.3        22.2        NFTS
 D:/          64.2         21.3       NFTS
 E:/          22.2         11.1       DVD

要求是,如果可用空间为 10% 或低于该行的字体应为红色,否则为绿色。这是我的代码:

<asp:DataList ID="DataList1" runat="server" BackColor="#FFFF99" 
  BorderColor="Black" BorderWidth="2px" CellPadding="4" Font-Bold="False" 
  Font-Italic="False" Font-Overline="False" Font-Size="Small" Font-Strikeout="False" 
  Font-Underline="False" RepeatDirection="Horizontal" style="z-index: 1; left: 421px; top: 137px; position: absolute; height: 132px; width: 495px" 
  ForeColor="#333333" GridLines="Vertical" DataSourceID="DriveInfo">

    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />

    <AlternatingItemStyle BackColor="#FFFBD6" Font-Bold="False" Font-Italic="False" 
    Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />

    <ItemStyle BackColor="#FFFBD6" Font-Bold="False" Font-Italic="False" 
    Font-Overline="False" Font-Strikeout="False" Font-Underline="False" ForeColor="#333333" />

    <SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />

    <ItemTemplate>
        DriveName:
        <asp:Label ID="DriveNameLabel" runat="server" style="font-weight: 700" Text='<%# Eval("DriveName") %>' />
        <br />

        Total: <b>
        <asp:Label ID="TotalLabel" runat="server" Text='<%# Eval("Total") %>' />
        &nbsp;GB</b><br />

        Free: <b>
        <asp:Label ID="FreeLabel" runat="server" Text='<%# Eval("Free") %>' />
        &nbsp;GB</b><br />

        Label:
        <asp:Label ID="LabelLabel" runat="server" style="font-weight: 700" Text='<%# Eval("Label") %>' />

        <br />
        <br />
    </ItemTemplate>
</asp:DataList>
4

1 回答 1

2

您可以像这样设置 asp:label 的 CssClass 属性:

<asp:Label ID="FreeLabel" runat="server" Text='<%# Eval("Free") %>' CssClass='<%# float.Parse(Eval("Free")) / float.Parse(Eval("Total")) < 0.10 ? "red" : "" %>' />

你还需要一个 CSS 类:

.red {
  color: red;
}
于 2012-05-17T07:40:18.417 回答