开发者

Why is img src not working as <asp:Image ImageUrl does

I'm currently trying to get an 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;" />

I can use the JavaScript to open th开发者_Go百科e window but the image isn't being found for the img.


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 = "...".


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜