1

I am trying to set the ImageUrl using the following code...

<asp:Image ID="brokenImage" runat="server" AlternateText="Coded path" ImageUrl='~/headerImages/<%= getImage(Request.QueryString["Id"]) %>'/>

Eliminating the opening tag and viewing the browser ouput alongside an image tag that is explicitly written produces the following results....

asp:Image ID="brokenImage" runat="server" AlternateText="Coded path" ImageUrl='~/headerImages/images (3).jpg' />

asp:Image ID="workingImage" runat="server" AlternateText="Explicit Path" ImageUrl="~/headerImages/images (3).jpg" /> 

Why is the first image not displaying but the second is? How can I pass a parameter to the code behind and set the url?

2
  • 2
    If the browser output includes asp:Image tags then something much more fundamental is broken here. That would indicate that your web server isn't doing server-side ASP.NET processing. Commented Jan 15, 2014 at 20:46
  • is query string holding the extension of the image like .jpg or id only ? Commented Jan 15, 2014 at 20:47

1 Answer 1

3

If you are looking for correct way to do this, your markup should be :

<asp:Image ID="brokenImage" runat="server" AlternateText="Coded path" ImageUrl='<%# "~/headerImages/" + getImage(Request.QueryString["Id"]) %>'/>

And in the code you should have this:

brokenImage.DataBind();

Explanation: <%= %> outputs directly to the response stream. And <%# %> evaluates the code when you bind the control. Very nice and short explanation here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.