Reputation: 19
I am struggling to set ImageURL in a asp:image control. I am setting it to a URL from another site, at the moment I have several versions of the image trying to get one to work bellow is my code and the output.
Function used in images bellow
Function BuildLogoPath() As String
Return ConfigurationManager.AppSettings("WebPath") & "/img/main/logo.jpg"
End Function
ASP.net Code in page
<asp:Image ID="imgFav" runat="server" ImageUrl='<%: ConfigurationManager.AppSettings("WebPath") & "img/favicon.ico" %>' /><br />
<asp:Image ID="Image2" runat="server" ImageUrl='<%= ConfigurationManager.AppSettings("WebPath") & "/img/main/logo.jpg" %>' />
<asp:Image ID="Image3" runat="server" ImageUrl='<% ConfigurationManager.AppSettings("WebPath") & "/img/main/logo.jpg" %>' />
<asp:Image ID="Image4" runat="server" ImageUrl='<% Response.write(ConfigurationManager.AppSettings("WebPath") & "/img/main/logo.jpg") %>' />
<asp:Image ID="imgLogo" runat="server" ImageUrl='<%# BuildLogoPath() %>' /><br />
<asp:Image ID="Image5" runat="server" ImageUrl='<%= BuildLogoPath() %>' />
<asp:Image ID="Image6" runat="server" ImageUrl='<% BuildLogoPath() %>' />
<asp:Image ID="Image7" runat="server" ImageUrl='<% response.write(BuildLogoPath()) %>' />
<%= ConfigurationManager.AppSettings("WebPath") & "img/favicon.ico" %><br />
<%= ConfigurationManager.AppSettings("WebPath") & "/img/main/logo.jpg" %>
Output:
<img id="ContentPlaceHolder1_imgFav" src="<%:%20ConfigurationManager.AppSettings("WebPath")%20&%20"img/favicon.ico"%20%>"><br>
<img id="ContentPlaceHolder1_Image2" src="<%=%20ConfigurationManager.AppSettings("WebPath")%20&%20"/img/main/logo.jpg"%20%>">
<img id="ContentPlaceHolder1_Image3" src="<%%20ConfigurationManager.AppSettings("WebPath")%20&%20"/img/main/logo.jpg"%20%>">
<img id="ContentPlaceHolder1_Image4" src="<%%20Response.write(ConfigurationManager.AppSettings("WebPath")%20&%20"/img/main/logo.jpg")%20%>">
<img id="ContentPlaceHolder1_imgLogo" src=""><br>
<img id="ContentPlaceHolder1_Image5" src="<%=%20BuildLogoPath()%20%>">
<img id="ContentPlaceHolder1_Image6" src="<%%20BuildLogoPath()%20%>">
<img id="ContentPlaceHolder1_Image7" src="<%%20response.write(BuildLogoPath())%20%>">
http://office.logma.biz/onefit.com.jamie/img/favicon.ico<br>
http://office.logma.biz/onefit.com.jamie//img/main/logo.jpg
As you can see the code is working fine when not not in the image control.
Upvotes: 0
Views: 1371
Reputation: 7401
The simple answer is that you can't use <%
and %>
inside <asp: .. />
tags. You have two options:
Set it programmatically from code-behind:
imgFav.ImageUrl = Me.BuildLogoPath()
Render a normal <img>
tag in the HTML instead:
<img src='<%= BuildLogoPath() %>' />
Upvotes: 1