日期:2014-05-18  浏览次数:20458 次

HtmlTextWriter显现 请教高手指点
上面用HtmlTextWriter呈现一张图1,中间显示部分html固定代码,接着又以HtmlTextWriter方式显现一张图2,试了一个下午都没有实现,请知道的帮忙。谢谢了。
-------------------------------------------
我想要结果如下:
HTML code

<body>
    <form id="form1" runat="server">
    <div>
    HtmlTextWriter writer显现方式-------下面显现图片1

<% =writer.AddAttribute("url", "../xxx.gif");
    writer.AddAttribute("width", "50");
    writer.AddAttribute("height", "50");
    writer.RenderBeginTag(HtmlTextWriterTag.Img);
    writer.RenderEndTag(); 
 %> 

中间html代码<br>-------下面显现图片2
<% =
    writer.WriteBeginTag("img");
    writer.WriteAttribute("alt", "A custom image.");
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.WriteEndTag("img");
    writer.WriteLine();  %> 
    </div>
    </form>
</body>



------解决方案--------------------
<form id="form1" runat="server">
<div>
HtmlTextWriter writer显现方式-------下面显现图片1

<%
System.IO.StringWriter stringWriter = new System.IO.StringWriter();

HtmlTextWriter writer = new HtmlTextWriter(stringWriter);

writer.AddAttribute("src", "http://dotnet.aspx.cc/Images/logoSite.gif");
writer.AddAttribute("width", "50");
writer.AddAttribute("height", "50");
writer.RenderBeginTag(HtmlTextWriterTag.Img);
writer.RenderEndTag();
 %> 

中间html代码<br>-------下面显现图片2
<% 
writer.WriteBeginTag("img");
writer.WriteAttribute("alt", "A custom image.");
writer.WriteAttribute("src", "http://dotnet.aspx.cc/Images/logoSite.gif");
writer.Write(HtmlTextWriter.TagRightChar);
writer.WriteEndTag("img");
writer.WriteLine();

Response.Write(stringWriter.ToString());
%> 
</div>
</form>