Blag
Blag

Reputation: 19

ASP.Net image set ImageURL to external site

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(&quot;WebPath&quot;)%20&amp;%20&quot;img/favicon.ico&quot;%20%>"><br>
<img id="ContentPlaceHolder1_Image2" src="<%=%20ConfigurationManager.AppSettings(&quot;WebPath&quot;)%20&amp;%20&quot;/img/main/logo.jpg&quot;%20%>">
<img id="ContentPlaceHolder1_Image3" src="<%%20ConfigurationManager.AppSettings(&quot;WebPath&quot;)%20&amp;%20&quot;/img/main/logo.jpg&quot;%20%>">
<img id="ContentPlaceHolder1_Image4" src="<%%20Response.write(ConfigurationManager.AppSettings(&quot;WebPath&quot;)%20&amp;%20&quot;/img/main/logo.jpg&quot;)%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

Answers (1)

Adrian Wragg
Adrian Wragg

Reputation: 7401

The simple answer is that you can't use <% and %> inside <asp: .. /> tags. You have two options:

  1. Set it programmatically from code-behind:

    imgFav.ImageUrl = Me.BuildLogoPath()

  2. Render a normal <img> tag in the HTML instead:

    <img src='<%= BuildLogoPath() %>' />

Upvotes: 1

Related Questions