How can I use the IF clause within the transformation?

Technical support leader

Juraj Ondrus asked on September 21, 2010 07:48

How can I use the IF clause within the transformation?

Correct Answer

Juraj Ondrus answered on September 21, 2010 07:48

If you want to check for example NewsTitle field if it contains the word: “Home” you can use the if condition like this:

<%# (Eval("NewsTitle").ToString().Contains("Home")) ? "go home" : "go away" %>

The output text is “go home” if the NewsTitle contains “Home” substring and “go away” if not. Of course, you can use any Boolean expression before the question mark.

-hg-
2 votesVote for this answer Unmark Correct answer

Recent Answers


Bill Gross answered on June 23, 2014 11:41

This answer doesn't really answer the question. While it is helpful to know how the unary operator would work, it doesn't address an actual "if" condition. I'm looking for something like:

<%# if (Eval("FormatAs").ToInt32() == 1) { %>

Show this headline

<%# } else { %>

Tell the user they're in the wrong place

<%# } %>

This code doesn't work, and I had hoped that this Q&A answer would help me learn why.

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on June 24, 2014 03:51

Hello,

the above question is 4 years old so it applied to a very old version of Kentico. Also, macros are not supported in ASCX transformations. You need to either use SCRIPT tags and use C# code or add custom function to the transformation that will do the logic you want.

Best regards, Juraj Ondrus

0 votesVote for this answer Mark as a Correct answer

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