1

For some reason the HtmlTextWriter is inserting weirdness into my html output. In the following code I try to create a clickable div that contains an image:

writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "location.href = '" 
    + TargetHTTPRef + "'");
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.AddAttribute(HtmlTextWriterAttribute.Src, ThumbFileName);
writer.RenderBeginTag(HtmlTextWriterTag.Img);
writer.RenderEndTag();

I get the following output:

<div class="leftGallery" onclick="location.href = &#39;http://www.google.com&#39;">
<img src="./images/services/47_Kayak%20Thumb.jpg" /><div class="galleryPanel">

So I'm getting &#39 and %20 occurring where the apostrophes and spaces should be in the output. Is there a way to stop this??

Paul Matthews
  • 2,164
  • 5
  • 20
  • 29

1 Answers1

2

Try another HtmlTextWriter.AddAttribute overload:

writer.AddAttribute(
    HtmlTextWriterAttribute.Onclick,
    "location.href = '" + TargetHTTPRef + "'",
    false);

writer.AddAttribute(HtmlTextWriterAttribute.Src, ThumbFileName, false);

The third argument to this overload indicates whether to encode the attribute and its value.

J0e3gan
  • 8,740
  • 10
  • 53
  • 80