Mihály Attila Mihály Attila - 3 months ago 14
ASP.NET (C#) Question

How to use Eval in ternary-operator, aspx page

So, I'm trying to display 2 different html codes depending on the bitColors value. I'm getting the given errors, how can it be solved only on the aspx page?

<%# Eval("bitColor").ToString() == "true"
? "<i class='fa fa-circle-o' style='color: rgb(<%# Eval("chrColorCode") %>)'></i><%# Eval("varDisplayName")%>"
: "<i class='fa fa-circle-o' style='display: none'></i><%# Eval("varDisplayName")%>"
%>



I get in
? "<i class='fa fa-circle-o' style='color: rgb(<%# Eval("chrColorCode") %>)'></i><%# Eval("varDisplayName")%>"
:



Compiler Error Message: CS1010: Newline in constant



or when hovering over the following code in vs:
Eval("chrColorCode")



"Syntax error, ":" expected"



Answer

This is the code that you need, you need not to wrap Eval again into c# code quotes once you have already opened it.

<%# Eval("bitColor").ToString() == "true" 
? ("<i class='fa fa-circle-o' style='color: rgb(" + Eval("chrColorCode") + ")'></i>" + Eval("varDisplayName")) 
: ("<i class='fa fa-circle-o' style='display: none'></i>" + Eval("varDisplayName")) %>