IF Statement in transformation

Alexander Toups asked on November 21, 2015 14:57

I'm trying to use a C# if-statement in a transformation of a basic repeater with a SQLDataSource, like this:

<% if(IsFirst()) { %>
        <p> Some text <%# Eval("MyField")%>
<% } %>

This works fine. But if it comes to checking agains a field value, like this:

<% if(Eval("MyField1").ToString()=="value") { %>
        <p> Some text <%# Eval("MyField2")%>
<% } %>

I'll get an server error. I want to avoid using K# statments like IfCompare, because then I have to put the whole output string into the function, which is becomes horrible when dealing with nested conditions.

Can anybody help?

Recent Answers

Brenden Kehren answered on November 21, 2015 16:52

Are you using an ASCX transformation or a text transformation? The syntax you're showing is ASCX and standard C# syntax.

0 votesVote for this answer Mark as a Correct answer

Alexander Toups answered on November 21, 2015 18:05

Yes, ASCX.

0 votesVote for this answer Mark as a Correct answer

Timothy Fenton answered on November 23, 2015 13:43 (last edited on December 10, 2019 02:30)

Hello Alexander you should really use K# for this as even something as basic as:

<% Eval("MyField1").ToString(); %>

will cause a server error to be thrown, we do not have full support for this in ascx transformations. That being said you could do almost the identical thing in K# like this:

{% if(MyField1 == "value") { %}
 <p> Some text {% MyField2 %}</p>
{% } |(identity)GlobalAdministrator%}

And it doesnt get any more messy than doing it in an in-line embedded code tag

Hope this works for you

1 votesVote for this answer Mark as a Correct answer

Joshua Myers answered on December 1, 2016 21:12 (last edited on June 12, 2018 13:19)

Using the ASCX transformation type, is there a way to wrap content in the IF statement that isn't all on one line?

This is how it is properly working right now:

<%# IfEmpty(Eval("ProgramCost"), "", "<h3>Estimated Program Cost</h3><p><strong>Total Tuition, Fees &amp; Books:</strong> " + Eval("ProgramCost") + "</p>")  %>

Here is how I'd like to be able to use it (but it currently gives me an error).



<h3>Estimated Program Cost</h3>
<p><strong>Total Tuition, Fees &amp; Books:</strong> <%# Eval("ProgramCost") %></p>



There has got to be a way to make this happen!

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.