view image in repeater

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 !
 
N

nicholas

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

THX
 
B

Ben Lucas

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;
}
 
S

Scott Allen

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,
 
N

nicholas

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:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top