Question

I'm working with datalists and this particular datalist I'm currently working on retrieves data from a database. It queries the drives of a particular PC in the database, and shows its Total and free space. Rows should look like this in the data list:

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

The requirement is, If the Free space is 10% or below the font of the row should go red, Green if not. Here is my code:

<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>
Was it helpful?

Solution

You could set the CssClass attribute of the asp:label like this:

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

You'd also need a css class:

.red {
  color: red;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top