Rod
Rod

Reputation: 15477

add color style to value based on condition

How do I conditionally add the style 'color:red' to the .CurrentDifference if the value happens to be a negative value?

Thanks, rod.

<div class="sRow">
    <div class="sLabel p40">
        Difference:
    </div>
    <%= (!String.IsNullOrEmpty(Model.Amount)?Model.Amount.CurrentDifference.ToString("c"):string.Empty) %>
</div>

Upvotes: 1

Views: 528

Answers (2)

John Hartsock
John Hartsock

Reputation: 86902

<div class="sRow"> 
    <div class="sLabel p40"> 
        Difference: 
    </div> 
    <span style="<%=Model.Amount.CurrentDifference < 0  ? "color: #FF0000": ""%>"> 
      <%= (!String.IsNullOrEmpty(Model.Amount)?Model.Amount.CurrentDifference.ToString("c"):string.Empty) %> 
    </span>
</div> 

Note this is very sloppy. I would consider putting the logic for this in your Controller Action instead of putting conditional logic in your View. Perhaps use tempdata or even expose a new field in your model.

Upvotes: 1

Jonathan Park
Jonathan Park

Reputation: 775

I would add a class to your style sheet for the red color. Then conditionally apply the class to a span as such.

<div class="sRow">
    <div class="sLabel p40">
        Difference:
    </div>
    <span class='<%= (Model.Amount>0?"Currency":"CurrencyRed") %>' >
    <%= (!String.IsNullOrEmpty(Model.Amount)?Model.Amount.CurrentDifference.ToString("c"):string.Empty) %>
    </span>
</div>

Upvotes: 1

Related Questions