Transformation If/Else Statement

Todd Campion asked on May 22, 2017 15:13

Hey Everyone, I know this has been answered already here, but I'm having the darndest time getting this thing to work. I have a custom table and a field called "Attachment_1" .. I simply want nothing to display if this field is empty and a hyperlink to the document to display if it is there. If I don't use and if/else the entire column gets the word attachment.. however only documents with attachments actually work.. so I know that part is good

<a href='<%# GetAbsoluteUrl("/getattachment/" + GetSearchValue("Attachment_1")) %>' title="Attachment" target="_blank">Attachment

When I put in the condition.. I'm not sure how to get the word only displayed when their is a value.. This is what I have.. but not sure how to get the value to display.. any help would be cool...

<%# IfEmpty(Eval("Attachment_1"), "",GetAbsoluteUrl("/getattachment/" + GetSearchValue("Attachment_1"))) %>

Recent Answers


Anton Grekhovodov answered on May 22, 2017 16:40

Hi Todd,

Your result expression looks correct, but you should check what Attachment_1 field holds and if the field is empty, what actual value is... null or empty string? Becausei if the first parameter is null, the method returns the second parameter, but if it's empty string, it will return the third parameter. This behavour is described in documentation

0 votesVote for this answer Mark as a Correct answer

Prashant Verma answered on May 22, 2017 18:01 (last edited on May 22, 2017 18:01)

Hi Todd,

Simply use ternary operator for checking both result either null or empty string, so your condition should be like this <%# String.IsNullOrEmpty(Eval("Attachment_1").ToString()) ? "" : GetAbsoluteUrl("/getattachment/" + GetSearchValue("Attachment_1")) %>

Thanks

Happy to help you

1 votesVote for this answer Mark as a Correct answer

Todd Campion answered on May 22, 2017 18:40

Thanks you so much... I feel I'm getting closer..I do appreciate the help... This is my code <a href='<%# String.IsNullOrEmpty(Eval("Attachment_1").ToString()) ? "" : GetAbsoluteUrl("/getattachment/" + GetSearchValue("Attachment_1")) %>' title="Attachment" target="_blank">Attachment

and I'm getting this error... This error appears only on objects that do not have an attachment... The ones that do seem to be working correctly... [CMSAbstractTransformation.DataBind]: Object reference not set to an instance of an object. 04/07/2017 Embedded2 Download Attachment Open

0 votesVote for this answer Mark as a Correct answer

Prashant Verma answered on May 22, 2017 18:43

Hi Todd,

<%# String.IsNullOrEmpty(Eval("Attachment_1")) ? "" : GetAbsoluteUrl("/getattachment/" + GetSearchValue("Attachment_1")) %>

I forget to remove type cast you just simply use above code. It will work.

Thanks

0 votesVote for this answer Mark as a Correct answer

Todd Campion answered on May 22, 2017 18:47

Again Thank you for your help.. this is my new error

[TempITemplate.Template]: http://server/CMSVirtualFiles/Transformations/=vg=6e736632-07bb-44ac-a5a1-94e61b80cd1f/Nashua.File/SearchFileList.ascx(46): error CS1502: The best overloaded method match for 'string.IsNullOrEmpty(string)' has some invalid arguments

using this code

<%# String.IsNullOrEmpty(Eval("Attachment_1")) ? "" : GetAbsoluteUrl("/getattachment/" + GetSearchValue("Attachment_1")) %>

0 votesVote for this answer Mark as a Correct answer

Prashant Verma answered on May 22, 2017 18:55 (last edited on May 22, 2017 18:59)

Hi Todd,

<%# Eval("Attachment_1")!=null ? String.IsNullOrEmpty(Eval("Attachment_1").ToString()) ? "" : GetAbsoluteUrl("/getattachment/" + GetSearchValue("Attachment_1")):"" %>

Really apologize I not having on my development machine to test such case's

But I believe it should work as expected

Thanks

0 votesVote for this answer Mark as a Correct answer

Todd Campion answered on May 22, 2017 19:02

I'm just pumped to be getting help.. so thankful... so.. I believe this works.. The documents without an attachement do not display anything.. but neither do the documents with attachements.. there is no clickable word... which makes sense because we just retrieved the document.. but the end user has nothing to click on... thanks for hanging in there with me

0 votesVote for this answer Mark as a Correct answer

Prashant Verma answered on May 22, 2017 19:07

Most welcome Todd do vote/mark answer if you feel my answer help you.

Thanks

Prashant :)

0 votesVote for this answer Mark as a Correct answer

Todd Campion answered on May 22, 2017 19:14

Almost .. I need the word "Attachment" to appear.. right now the table displays nothing .. whether there is an attachment or not.. How do I add a link to this bad boy? Thank you!!

0 votesVote for this answer Mark as a Correct answer

Anton Grekhovodov answered on May 23, 2017 08:17

When I need to show/hide some HTML markup, I prefer to use another way for conditional statement insted of IfEmpty method, because IfEmpty method is looked too complicated then.

<asp:PlaceHolder runat="server" Visible='<%# Eval("NodeAliasPath") != null && !String.IsNullOrEmpty(Eval("NodeAliasPath").ToString()) %>'>
  <a href="<%# GetAbsoluteUrl("/getattachment/" + GetSearchValue("NodeAliasPath")) %>">
    Attachment
  </a>
 </asp:PlaceHolder>

Inside placeholder, you will have clear HTML markup and you can modify/format it easily.

1 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on May 25, 2017 03:05

After talking with Todd on the phone it seems the issue is with the way he was evaluating the column. Since he was in a smart search results transformation the Eval() method does not work. So this statement:

<%# IfEmpty(Eval("Attachment_1"), "",GetAbsoluteUrl("/getattachment/" + GetSearchValue("Attachment_1"))) %>

needed to be updated to:

<%# IfEmpty(GetSearchValue("Attachment_1"), "",GetAbsoluteUrl("/getattachment/" + GetSearchValue("Attachment_1"))) %>

0 votesVote for this answer Mark as a Correct answer

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