asp.net 输出 页面内容 在服务器上

1.定义页面内容

按 Ctrl+C 复制代码
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

<asp:Panel ID="resultDiv" runat="server">
    <h2>
        歡迎使用 ASP.NET!
    </h2>
    <p>
        若要進一步了解 ASP.NET,請造訪 <a href="http://www.asp.net" title="ASP.NET 網站">www.asp.net</a><asp:TextBox ID="TextBox1" runat="server" Text="gsw"></asp:TextBox>
    </p>
    <p>
        您也可以尋找 <a href="http://go.microsoft.com/fwlink/?LinkID=152368"
            title="MSDN ASP.NET 文件">MSDN 上有關 ASP.NET 的文件</a></p>
</asp:Panel>
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</asp:Content>
按 Ctrl+C 复制代码2.重写 Render 方法
  代码如下:

按 Ctrl+C 复制代码
protected override void Render(HtmlTextWriter writer)
        {


            StringWriter sw;
            HtmlTextWriter htmltw;
            sw = new StringWriter();
            htmltw = new HtmlTextWriter(sw);
            base.Render(htmltw);
            //htmltw = writer;
            StringBuilder temp = sw.GetStringBuilder();
            //base.Render(writer);
            string PageResult = temp.ToString();   

            writer.Write(PageResult);
            System.IO.StreamWriter strWrite = new StreamWriter(Server.MapPath("a.html"));
            strWrite.Write(PageResult);
            strWrite.Close();

 
         
        }
按 Ctrl+C 复制代码上面的代码,是整个页面输出到html。

3.可以指定服务器控件的内容,输出成html。

3.1 需要重写方法VerifyRenderingInServerForm

代码如下:

      public override void VerifyRenderingInServerForm(Control control)
        {
            return;
        }3.2 重写 Render 方法


 protected override void Render(HtmlTextWriter writer)
        {


   

            base.Render(writer);
            System.Text.StringBuilder strBuilder = new System.Text.StringBuilder();
            System.IO.StringWriter oStringWriter = new
            System.IO.StringWriter(strBuilder);
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new
            System.Web.UI.HtmlTextWriter(oStringWriter);
            strBuilder.Append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>首頁</title></head><body>");
            resultDiv.RenderControl(oHtmlTextWriter);
            System.IO.StreamWriter strHtmlFile = System.IO.File.CreateText(Server.MapPath( "a.html"));
            strBuilder.Append("</body></html>");
            strHtmlFile.WriteLine(strBuilder.ToString());
            strHtmlFile.Close();
         
        }

 

posted @ 2015-01-14 15:40  xw2cc  阅读(271)  评论(0编辑  收藏  举报