Change HTML DIV Color based on condition inside ASP.Net User Control

slb2013
 
on Aug 25, 2022 10:41 PM
Sample_103818.zip
304 Views

Change HTML DIV Color based on condition in ASP.Net

The code looks good as good example. Thanks. 

But my code is already created inside of Testing.ascx for the signalTest.

I just want to make if signalTest = "PASS" will be display text "PASS" in green color else signalTest FAIL will display text FAIL in red color.

Please see below:

<span><%=signalTest%> + "(" + message.SignalStrength.ToString() + "/" + maxRSSI.ToString() + ")" %></span>

PASS (-35/-66) in green text

FAIL (-17/-11) in red text

Download FREE API for Word, Excel and PDF in ASP.Net: Download
PrinceG
 
on Aug 26, 2022 03:26 AM

Hi slb2013,

Please refer below sample.

HTML

Default

<uc:Signal runat="server" ID="ucSignal" />

User Control

<%foreach (Signal message in this.SignalData)
  {%>
<span class='<%=message.SignalStrength=="Pass"?"Green":"Red" %>'>
    <%=message.SignalStrength%></span><br />
<%} %>
<style type="text/css">
    .Green
    {
        font-family: Arial;
        font-size: 10pt;
        color: green;
    }
    .Red
    {
        font-family: Arial;
        font-size: 10pt;
        color: red;
    }
</style>

Signal Class

C#

public class Signal
{
    public string SignalStrength { get; set; }
    public int Percent { get; set; }
}

Code

UserControl

public List<Signal> SignalData { get; set; }

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        ucSignal.SignalData = new List<Signal>()
        {
            new Signal{ SignalStrength="Fail",Percent=10},
            new Signal{ SignalStrength="Pass",Percent=80},
        };
    }
}

 Screenshot