Joseph
Joseph

Reputation: 661

Simple If Then statement not working the way I think it should

I'm relatively new to VB and I'm being forced to use the in-line coding method. I would like to check for a not null value for a variable and then display an image. That's all I want it to do. Here's a couple of instances of what I've tried to do.

<% if ACTL is not nothing then %>
<img id="ACTLLogo" src="<%= ACTL %>" />
<% else end if %>

or this:

<% if ACTL is nothing then %>
&nbsp;
<% else %>
<img id="ACTLLogo" src="<%= ACTL %>
<% end if %>

or this:

<% if String.IsNullOrEmpty(ACTL) then %>
&nbsp;
<% else %>
<img id="ACTLLogo" src="<%= ACTL %>
<% end if %>

When I do just the part without the if statements, the logo shows up fine, so I'm thinking I just don't know how to do in-line coding IF statements in ASP Classic. Any thoughts?

Upvotes: 1

Views: 90

Answers (2)

Lima
Lima

Reputation: 1211

In ASP Classic you can use <% if ACTL <>"" %>

You could also write your code like this, which removes the need to close the asp tag.

<% 
if ACTL <>"" then
response.write("<img id=""ACTLLogo"" src="""&ACTL&""" />")
end if
%>

Might be a bit easier to read and maintain.

Upvotes: -1

Martha
Martha

Reputation: 3854

To check whether a variable is NULL, use the IsNull function:

If Not IsNull(ACTL) Then
    Response.Write "<img src='" & ACTL & "' id='ACTLLogo'>"
End If

You can also check whether the variable is EMPTY (which is what you get if, for example, you do a Request.Form("fld") and the form does not have a field called fld) using the, you guessed it, IsEmpty function:

If IsEmpty(Request.Form("fld")) Then
    Response.Write "<p class='msg'>No such field!</p>"
End If

If all you care about is whether the variable has a value, any value, then you can do a quick-and-dirty string conversion:

If ACTL & "" <> "" Then
    ...
End If

This latter trick is very useful for avoiding the type mismatch errors you can get if a variable is unexpectedly NULL, without having to add If Not IsNull(...) all over the place.

Upvotes: 1

Related Questions