view image in repeater

  • Thread starter Thread starter nicholas
  • Start date Start date
N

nicholas

Got an asp.net data-repeater on my page.
I can view the texts from the database (ex.: <%#
Databinder.Eval(Container.DataItem, "contentEN") %> ), but I also would like
to see the image, but only if there is one.

So in the repeater there should be someting like:

if ( the_image_field <> "" ) then
show the image with the filename from the database field
end if

THX !
 
In fact the problem can be reduced to:
How to use an if statement in a repeater control ??

THX
 
You will need to handle the ItemDataBound event to do this. I would go
ahead and place an image control in the repeater and bind it to the field
you use to determine the path to the image. Then in the ItemDataBound
event, you can do something like the following:

void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Image myImage = e.Item.FindControl("myImage"); //This should be the
name of the Image control
if (myImage.ImageUrl == string.empty) // ImageUrl would be the field we
bound
myImage.Visible = false;
}
 
Hi nicholas:

In addition to the ItemDataBound event, which is a great suggestion
Ben mentioned, there are ways to use methods inside of data binding
expressions. You can use just about anything there once you realize
how ASP.NET uses the expression when it gens code.

I think my article provides some insight:

Digging into DataBinding Expressions
http://odetocode.com/Articles/278.aspx

HTH,
 
THX everyone for advice.

Here is my working code:

Serverside script:
-----------------

Function imageExists(imagefield as String) as String
If (imagefield) <> "" then
Return "<img name=""image1"" src=""/files/file1/" & imagefield & """
alt="""" />"
Else
End If
End Function

And in my HTML:
 
Back
Top