ASCX Transformation If Statement

Brendon McCarthy asked on October 23, 2014 00:05

How do you create a conditional if statement in ASPX transformations? The content within the if and else is multiple lines and contains additional data fields. I'm following standard in-line ASP.NET code practice to my knowledge.

The odd thing is this line Convert.ToBoolean(IfImage("NewsTeaser", "true", "false")).ToString() when placed inside a <%#...%> tag displays the text "True" in the transform, but when evaluated as part of the if statement, it does not. I've attempted with or without converting a string, as a bool it also does not evaluate.

...
<% if (Convert.ToBoolean(IfImage("NewsTeaser", "true", "false")).ToString() == "True") { %>
    <div class="col-md-3">
      <a href="<%# GetDocumentUrl() %>" title="<%# Eval("NewsTitle") %><%# Eval("ArticleName") %>">
        <img src="<%# IfImage("NewsTeaser", GetFileUrl("NewsTeaser"), "") %><%# IfImage("ArticleTeaserImage", GetFileUrl("ArticleTeaserImage"), "") %>" class="img-responsive" alt="<%# Eval("NewsTitle") %><%# Eval("ArticleName") %>" title="<%# Eval("NewsTitle") %><%# Eval("ArticleName") %>">
      </a>
    </div>
    <div class="col-md-9">
<% } else { %>
    <div class="col-md-12">
<% } %>
...

My question is similar to these, which were never answered 100%:

http://devnet.kentico.com/questions/if-else-in-transformation-with-multiple-lines

http://devnet.kentico.com/questions/how-can-i-use-the-if-clause-within-the-transformation- (comment by Bill)

The end goal is to check if an image exists for either as ArticleTeaserImage or NewsTeaser (the repeater is loading content from both types, the above example only checks NewsTeaser currently). If neither exists, then the HTML for the image (and containing column) is not generated.

Recent Answers


Jim Spillane answered on October 23, 2014 02:02

you can give something like this a try...

<asp:PlaceHolder runat="server" Visible='<%# IfImage("NewsTeaser", true, false) %>'>
    <div class="col-md-3">
        <a href="<%# GetDocumentUrl() %>" title="<%# Eval("NewsTitle") %><%# Eval("ArticleName") %>">
        <img src="<%# IfImage("NewsTeaser", GetFileUrl("NewsTeaser"), "") %><%# IfImage("ArticleTeaserImage", GetFileUrl("ArticleTeaserImage"), "") %>" class="img-responsive" alt="<%# Eval("NewsTitle") %><%# Eval("ArticleName") %>" title="<%# Eval("NewsTitle") %><%# Eval("ArticleName") %>">
        </a>
    </div>
    <div class="col-md-9">
</asp:PlaceHolder>

<asp:PlaceHolder runat="server" Visible='<%# IfImage("NewsTeaser", false, true) %>'>
    <div class="col-md-12">
</asp:PlaceHolder>
1 votesVote for this answer Mark as a Correct answer

Brendon McCarthy answered on October 23, 2014 17:07

Thanks Jim. I don't love it, but it does work! Any reason why standard ASPX if statements do not work in this case?

I also need to check for ArticleTeaserImage in addition to NewsTeaser. I came up with this for the first placeholder and it works as expected: <asp:placeholder runat="server" visible='<%# IfImage("NewsTeaser", true, IfImage("ArticleTeaserImage", true, false)) %>'>

But on the second placeholder, it doesn't like the inverse. If neither exists, it does return true, but if either exists, the div-col-md-12 is visible: <asp:placeholder runat="server" visible='<%# IfImage("NewsTeaser", IfImage("ArticleTeaserImage", false, true), true) %>'> <div class="col-md-12"> </asp:placeholder>

This however does work, but unsure why the above does not: <asp:placeholder runat="server" visible='<%# IfImage("NewsTeaser", false, true) %>'> <asp:placeholder runat="server" visible='<%# IfImage("ArticleTeaserImage", false, true) %>'> <div class="col-md-12"> </asp:placeholder> </asp:placeholder>

0 votesVote for this answer Mark as a Correct answer

Jim Spillane answered on October 24, 2014 03:58

I feel your pain :) I believe the reason is that you can only use databinding expressions <%# %> in the transformation rather than the <% %> expressions.

If you can change the transformation to a Text/XML type, you can use k# for the if-else construct. this would be my preference. See Conditional statements

For another option, you can add a function to the transformation to help sort the logic...

<script runat="server">   
  public bool IsNewsTeaserandArticleTeaserImage()
    {      
      return (bool)IfImage("NewsTeaser", true, false) && (bool)IfImage("ArticleTeaserImage", true, false);
    } 
</script>

<asp:PlaceHolder runat="server" Visible='<%# !IsNewsTeaserandArticleTeaserImage() %>'>
    <div class="col-md-12">
</asp:PlaceHolder>
1 votesVote for this answer Mark as a Correct answer

Brendon McCarthy answered on November 5, 2014 22:30

I've switched to using Text/XML with Macros to test a better way. However, not all the ASPX methods appear to work properly. For example, IfEmpty never returns false. Many of the DateTime methods also do not work like GetCustomDateTime.

How do you check using the IF/conditional statements whether a field is null/blank?

0 votesVote for this answer Mark as a Correct answer

Jan Hermann answered on November 25, 2014 15:53 (last edited on December 10, 2019 02:30)

Hello,

Since you already have the Text/XML transformation type, you don't have to use IfEmpty methods anymore and you can simply compare it to empty strings:

{%SomeFiled.ToString()==""?"empty":"not empty"|(identity)GlobalAdministrator%}

Best regards,
Jan Hermann

1 votesVote for this answer Mark as a Correct answer

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