Geeks With Blogs
urig Tidbits from a .net life

Just bumped into another one of ASP.Net's annoying quirks. When setting the NavigateUrl property of an <asp:image>, ASP.Net automatically "HTMLEncodes" the value assigned. So if you'd try to pass a query string in the url, like so:
    <asp:image id="imgExample" runat="server" navigateurl = "example.jpg?foo=bar&bar=bie" />
you'll end up with something like this on the client side:
    <img id="imgExample" src="example.jpg?foo=bar&amp;bar=bie" />
which will thoroughly break your query string.

Note that this also happens when you try to set the NavigateUrl property programmatically.

The workaround I've come up with is to replace the <asp:image> with an <img> tag. It's not as obvious as it sounds when you're trying to set the url from the server side.

If you want to programmatically set the src attribute on an <img> tag from the server side, you'll need to know three things:

  1. To make the <img> tag a proper server side control you'll need to add a runat="server" attribute to it.
  2. On the server side, <img> tags are represented as System.Web.UI.HtmlControls.HtmlImage objects instead of the usual System.Web.UI.WebControls.Image object.
  3. The HtmlImage object has the property Src instead of the Image object's NavigateUrl.

Good luck!

Posted on Tuesday, September 19, 2006 4:30 PM Knowledge Base , Developer Tools | Back to top

Comments on this post: Workaround: How to set an ASP.Net Image's NavigateUrl property without having it HTMLEncoded.

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © urig | Powered by: