Transformations and if else statements

Shawn Ebeyer asked on December 22, 2015 17:38

Hi there,

I'm working on a transformation and need to include an if statement that if true, will return a bunch of HTML elements, but if not true, will return a whole different set of HTML elements.

I would like to check if a field is empty really. If it is, I will return one set of <li>'s.

I've looked around for answers but nothing I've come across so far has worked.

Thanks in advance,

Shawn

Recent Answers


Brenden Kehren answered on December 22, 2015 17:43

What I do for this is create a placeholder and set the visible property of it. In the example below, I wasn't able to use the content before and after on the webpart so I simply created placeholders with what I needed:

<asp:PlaceHolder ID="ph1" runat="server" Visible='<%# IsFirst() %>'>
<div class="unigrid-content">
  <table class="table">
    <thead>
      <tr class="unigrid-head">
        <th><a href="<%# CurrentDocument.NodeAliasPath %>?searchtext=<%# Request.QueryString["searchtext"] %>&searchmode=<%# Request.QueryString["searchmode"] %>&sort=<%#  "FileName+" + GetSortDirection() %>">File</a></th>
        <th><a href="<%# CurrentDocument.NodeAliasPath %>?searchtext=<%# Request.QueryString["searchtext"] %>&searchmode=<%# Request.QueryString["searchmode"] %>&sort=<%#  "FileDate+" + GetSortDirection() %>">Date</a></th>
        <th>Description</th>
        <th>Tagged As</th>
        <th>Download</th>
      </tr>
    </thead>
    <tbody>
</asp:PlaceHolder>
      <tr>
        <td><%# GetSearchValue("FileName") %></td>
        <td><%# FormatDateTime(GetSearchValue("FileDate"), "MM/dd/yyyy") %></td>
        <td><%# GetSearchValue("FileShortDescription") %></td>
        <td><%# GetSearchValue("DocumentTags") %></td>
        <td><a href='<%# GetAbsoluteUrl(GetSearchValue("FileUpload").ToString()) %>' title="Download" target="_blank">Download</a></td>
      </tr>
<asp:PlaceHolder ID="ph2" runat="server" Visible='<%# IsLast() %>'>
      </tbody>
    </table>
  </div>
</asp:PlaceHolder>
3 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on December 22, 2015 17:47

In addition to what Brenden advised, check IF statement in documentation: <%# If(Eval("IsSecuredNode"), "The page requires authentication", "The page is publicly available") %>

2 votesVote for this answer Mark as a Correct answer

Shawn Ebeyer answered on December 22, 2015 18:16

Thank you Brenden and Roman,

I'm still having issues getting it to work...

This is my starting point ...

 <%# IfEmpty(Eval("BuildingName")
 generate a bunch of <li> tags in a list

Then no clue how to add an else statement. Not really understanding the syntax too well, but your answers are very much appreciated.

Thanks,

Shawn

0 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on December 22, 2015 19:36

Shawn, in your particular case I'd do something like Brenden suggested:

<div style="<%# IfEmpty(Eval("BuildingName"), "display:none;", "display:block;") %>">
  ... put whatever layout you need when there is building name....
</div>

However, if there is a lot of HTML in that div it would be better to use PlaceHolder control from the performance stand point. I guess this would work:

<asp:PlaceHolder ID="ph1" runat="server" Visible='<%# IfEmpty(Eval("BuildingName"), false, true) %>'>
    ... put whatever layout you need when there is building name....
</asp:PlaceHolder>
2 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on December 23, 2015 14:34

@Shawn, the easiest way to handle it is to use placeholders and check the values. Attempting to use if()else in a transformation is better served using a custom transformation method. In my example, the if is in the placeholder and the else wasn't in a placeholder. So I was assuming the else was the default or false value.

<asp:PlaceHolder ID="ph1" runat="server" Visible='<%# IfEmpty(Eval("BuildingName"), false, true) %>'>
  <ul class="something">
</asp:PlaceHolder>
  <li><%# Eval("YourColumn") %></li>
<asp:PlaceHolder ID="ph2" runat="server" Visible='<%# IfEmpty(Eval("BuildingName"), false, true) %>'>
  </ul>
</asp:PlaceHolder>

In this example, it will always render the <li> tags but on render the opening and closing <ul> tags when the column BuldingName is not empty.

1 votesVote for this answer Mark as a Correct answer

Shawn Ebeyer answered on December 23, 2015 16:46

Hi Brendan and Roman,

I've used the placeholder method and its working really nicely, thank you. I'm still trying to wrap my head around it and am having some concatenation issues but overall it seems to be producing some good results...

Thank you again, and much appreciated.

Cheers,

Shawn

0 votesVote for this answer Mark as a Correct answer

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