tags:

views:

44

answers:

2

Hi,

I'm currently trying to get a img tag to show the image by settings its src property.

I was using an asp:ImageButton as:

asp:ImageButton ID="image" runat="server" ImageUrl='<%# "Images/" + Eval("CarMakeId") + ".jpg" %>' AlternateText="No image found" />

This showed the image fine but I wanted to do some javascript for clicking the imagebutton. I understand you can't set the 'Click' property to be javascript but only code behind so I changed to a standard html img:

img alt="No image found" src='<%# "Images/" + Eval("CarMakeId") + ".jpg" %>' onclick="openWindow('Test Message'); return false;" />

Now I can use the javascript to open the window but the image isn't being found for the img.

Could someone please tell me what I'm doing wrong causing the image not to display

Thanks!

Mr Cricket

+1  A: 

Try adding the attribute runat="server" to your <img ... /> element. This will then be available as an HtmlImage in your class, so if you declare it as HtmlImage myimg, you can set its source attribute in the class by myimg.Src = "...".

ars
I should have mentioned that I'm using this withing a Grid so I'm getting the CarMakeId from the grid element which could be different for each row. I think you're suggesting to set it in the code-behind? Am I correct in saying if I was setting it in the code-behind I wouldn't be able to set the CarMakeId?
Mr Cricket
In taht case you'll need to set it in the ItemDataBound event. See this page for details: http://www.codeproject.com/KB/aspnet/ImageFromDBtoGrid.aspx
ars
A: 

Solved this by putting an asp:Image inside the link so didn't have to set the OnClientClick for an image button. Image is now part of the link and has the same event for the click of the link. Makes the code more maintainable also.

Mr Cricket