Click to See Complete Forum and Search --> : A little confused...


tallpaul
February 17th, 2006, 07:58 AM
I have some classic asp that i am trying to convert to asp.net

I have started using item templates with the following code to call database feilds:<%# DataBinder.Eval(Container.DataItem, "Addressline1") %>

My problem is that i need to have an if then statment. If an image isnt present then load noimage.gif for instance:

<% if rs("CBanner") <> "" then %>
<td width="526" colspan="3" class="tablecells"><img src="images/DirectoryBanners/<%=cbanner%>" width="522"/></td>
<%else%>
<td width="526" colspan="3" class="tablecells">&nbsp;</td>
<%end if%>

How on earth do i do this with::<%# DataBinder.Eval(Container.DataItem, "Addressline1") %>


or am imissing somthing

Cheers for any help

mmetzger
February 17th, 2006, 10:30 AM
The easiest method is to create a method that takes the content as a parameter and returns the correct string.

For example:

In the <script runat="server"> (or codebehind) section:


private string formatImage(string imagelocation)
{
string retval;
// Check if it exists - you may need to modify this if a different value is used
// ie, an empty string, etc.
if (imagestring != null)
{
retval = "<img src=\"images/DirectoryBanners/" + imagelocation + "\" width=\"522\"/>";
} else {
retval = "&nbsp;";
}
return retval;
}


Then in your table do this:


<td width="526" colspan="3" class="tablecells">
<%# formatImage(DataBinder.Eval(Container.DataItem, "Addressline1").ToString()) %>
</td>