if else Statement

Mitra Foden asked on October 2, 2014 10:48

Hello,

I am not familiar to kentico. But now at work i have to fix something for another website.in the page, they want an a tag do nothing if there is no link, meaning doesn't open another page or anything else. I found the code and it's in document type, an ASCX file. I just need to put a condition that if there is nothing under the link, then do nothing. but the problem is this if statement doesn't work. I have change it to different formats but it doesn't realise the link is empty.

Does anyone know if there is another kind of if statement or condition which will recognise the empty field?

<% string url = Convert.ToString(Eval("URL"));
if(!string.IsNullOrEmpty(url)){ %> <a href='http://<%# Eval("URL") %>' target='_blank'><%# Eval("Title") %> <% } else { %>

 <a href='' onclick='return false;'><%# Eval("Title") %></a>

<% } %>

Correct Answer

Dawid Jachnik answered on October 3, 2014 11:58

You can do this by this way:

<table cellspacing="0" cellpadding="0" class="test">
        <tr><td VALIGN="TOP">      

            <%# IfEmpty(Eval("URL"),"","<a href='http://"+ Eval("URL") +"' target='_blank'>"+ Eval("Title") +"</a>") %>
</td></tr>
        <tr><td ALIGN="CENTER" VALIGN="MIDDLE">
             <%# IfEmpty(Eval("URL"),"","<a href='http://"+ Eval("URL") +"' target='_blank'>"+ GetImage("Icon") +"</a>") %>
</td></tr>

or by this way for whole table:

<%# IfEmpty(Eval("URL"),"","<table cellspacing='0' cellpadding='0' class='test'><tr><td VALIGN='TOP'><a href='http://"+ Eval("URL") +"' target='_blank'>"+ Eval("Title") +"</a></td></tr><tr><td ALIGN='CENTER' VALIGN='MIDDLE'><a href='http://"+ Eval("URL") +"' target='_blank'>"+ GetImage("Icon") +"</a> </td></tr></table>") %>

This should work correct. Please make sure that's the URL is null or empty

2 votesVote for this answer Unmark Correct answer

Recent Answers


Dawid Jachnik answered on October 2, 2014 13:49

Hello, In our scenario you can use this:

<%# IfEmpty(Eval("URL"), "empty url", "url not empty") %>

more Boolean operations avalible in Kentico you can find in the following post: Boolean operations

2 votesVote for this answer Mark as a Correct answer

Tim Spring answered on October 2, 2014 14:02

Hi,

There is an IfEmpty function in Transforms.

https://docs.kentico.com/display/K81/Reference+-+Transformation+methods

So something like...

<% IfEmpty(Eval("URL"),"<a href=\"\" onclick=\"return false;\">" + Eval("Title") + "</a>","<a href=\"http://"+ Eval("URL") + "\" target=\"_blank\">" + Eval("Title") + "</a>")) %>
0 votesVote for this answer Mark as a Correct answer

Mitra Foden answered on October 2, 2014 14:34

I still get error. missing; error. I tried ' instead of " also, but it's not working. Thanks a lot though, Mitra

0 votesVote for this answer Mark as a Correct answer

Dawid Jachnik answered on October 2, 2014 15:01

You should add # after the begining <% and it should work

<%# IfEmpty(Eval("URL"),"<a href=\"\" onclick=\"return false;\">" + Eval("Title") + "</a>","<a href=\"http://"+ Eval("URL") + "\" target=\"_blank\">" + Eval("Title") + "</a>")) %>
2 votesVote for this answer Mark as a Correct answer

Mitra Foden answered on October 2, 2014 15:16

Nope - still not working :(

0 votesVote for this answer Mark as a Correct answer

Martin Danko answered on October 2, 2014 15:51

Hello Mitra,

Please post here the code you are using right now.

Best regards, Martin

0 votesVote for this answer Mark as a Correct answer

Mitra Foden answered on October 3, 2014 10:25

right now I am using:

<%# Eval("Title") %>
<%# GetImage("Icon") %>

I would like to check the url first and if it's empty, then do nothing. I put so many different If statements, but none of them really works.

Many thanks, Mitra

0 votesVote for this answer Mark as a Correct answer

Mitra Foden answered on October 3, 2014 10:26 (last edited on October 3, 2014 11:33)

<table cellspacing="0" cellpadding="0" class="test">
            <tr><td VALIGN="TOP">      
         <a href='http://<%# Eval("URL") %>' target='_blank'><%# Eval("Title") %></a>
    </td></tr>
            <tr><td ALIGN="CENTER" VALIGN="MIDDLE">
                     <a href='http://<%# Eval("URL") %>' target='_blank'><%# GetImage("Icon") %></a>
    </td></tr>
</table>
0 votesVote for this answer Mark as a Correct answer

Mitra Foden answered on October 3, 2014 10:31

Oh, I'm not sure why I cannot send you my code.

0 votesVote for this answer Mark as a Correct answer

Mitra Foden answered on October 3, 2014 10:32

<a href='http://<%# Eval("URL") %>' target='_blank'><%# Eval("Title") %>

0 votesVote for this answer Mark as a Correct answer

Martin Danko answered on October 3, 2014 11:35

Hi Mitra,

I've changed the formatting of your message.. as you can see, it's possible to post a code here but you need to use the formatting of editor and wrap a whole code into "preformatted code", so simply select all the code and click on related button. I would also recommend you to use Preview button before submitting your answer.

Best regards, Martin

0 votesVote for this answer Mark as a Correct answer

Mitra Foden answered on October 3, 2014 12:26

Oh thanks a lot - It's working now.

Many many thanks!

0 votesVote for this answer Mark as a Correct answer

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