由于某种原因,HtmlTextWriter将奇怪的内容插入到我的html输出中。在下面的代码中,我尝试创建一个包含图像的可点击div:
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "location.href = '"
+ TargetHTTPRef + "'");
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.AddAttribute(HtmlTextWriterAttribute.Src, ThumbFileName);
writer.RenderBeginTag(HtmlTextWriterTag.Img);
writer.RenderEndTag();
我得到以下输出:
<div class="leftGallery" onclick="location.href = 'http://www.google.com'">
<img src="./images/services/47_Kayak%20Thumb.jpg" /><div class="galleryPanel">
所以我得到&amp;#39和%20出现在撇号和空格应该在输出中的位置。有办法阻止这个吗?
答案 0 :(得分:2)
尝试其他HtmlTextWriter.AddAttribute
overload:
writer.AddAttribute(
HtmlTextWriterAttribute.Onclick,
"location.href = '" + TargetHTTPRef + "'",
false);
writer.AddAttribute(HtmlTextWriterAttribute.Src, ThumbFileName, false);
此重载的第三个参数指示是否对属性及其值进行编码。