Compare Two Eval values in a conditional operator

2019-06-11 21:37发布

问题:

I tried:

<option value="<%# Eval("DataID")%>" selected="(<%# Eval("NewID")%>==<%# Eval("DataID")%>)?'selected':''"> <%# Eval("ColorName")%></option>

but output i am getting(inspect element)

<option value="13047" selected="(13050==13047)?'selected':''"> ..BLACK</option>

<option value="13048" selected="(13050==13048)?'selected':''"> .CHARCOAL</option>

<option value="13049" selected="(13050==13049)?'selected':''"> BANANA</option>

Is there any other way to use conditional operator or If condition in Repeater

回答1:

Try this:

selected='<%# Eval("NewID").ToString() == Eval("DataID").ToString() ?  "selected" : "" %>'

EDIT:

Well, my html is a bit rusty :=) Although the above code is syntactically correct it will not produce the desired result, i.e. to get the specified option selected. Sth like this is most probably what the OP wants to achieve:

<option value='<%# Eval("DataID")%>' '<%# Eval("NewID").ToString() == Eval("DataID").ToString() ?  "selected" : "" %>'>


回答2:

try in this way

selected='<%= Eval("NewID")==Eval("DataID")) ? String.Format("{0}","selected") : String.Empty %>'


回答3:

In the HTML scriplet, you need to be consistent about single quote and double quotes.

Basically you need to execute:

    Eval("NewID") == Eval("DataID") ? "Your True Value" : "Your False Value"

So, decorate about statement with the scriplet and ' - single quote

    '<%# Eval("NewID") == Eval("DataID") ? "Your True Value" : "Your False Value" %>'

For the syntax of Eval() in HTML(aspx) pages:

http://msdn.microsoft.com/en-us/library/4hx47hfe(v=vs.110).aspx



标签: c# repeater