Compare Two Eval values in a conditional operator in Repeater c#

流过昼夜 提交于 2019-12-08 12:44:06

问题


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



来源:https://stackoverflow.com/questions/27693624/compare-two-eval-values-in-a-conditional-operator-in-repeater-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!