Chris
Chris

Reputation: 3129

How to set conditions based on Eval() value?

I have this code

<div class="result correct"><%# Eval("QandAID") %></div>

and I am wondering how I can set conditions on the value, I.e if the eval value is 2 change div class to "result incorrect" else leave as "result correct". That's also part of the question if anyone knows how to do that (change the div class based on the condition), then that would be a bonus.

Oh and I have that code inside a repeater bound to a dataset.

Upvotes: 0

Views: 2672

Answers (2)

Simon Whitehead
Simon Whitehead

Reputation: 65077

This should happen outside of the markup. Make the class a property of your model and set it based on your condition:

class YourModel {
    public int QandAID { get; set; }
    public string ValidityClass {
        get {
            return QandAID == 1 ? "correct" : "incorrect";
        }
    }
}

Then your repeater template becomes something like this:

<div class='result <%# Eval("ValidityClass") %>'><%# Eval("QandAID") %></div>

Upvotes: 1

pid
pid

Reputation: 11607

Define a property in the Page class:

public int MyValue { get; set; }

Then access it in the page this way:

<div style='width: <%=MyValue %>px'></div>

This example should answer indirectly your question and open up some more possibilities on how to put values into HTML that may or may not be bound to a DataRow.

Another example:

<%# Eval("QandAid") == 2 ? "result incorrect" : "result correct" %>

Or:

<div class='<%# Eval("QandAid") == 2 ? "class1" : "class2" %>'>

Upvotes: 2

Related Questions