ASP.NET 中如何對生成的 HTML 內容流進行控制?

<%@ Page Language="C#" %>  
<%@ Import Namespace="System.IO" %>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<script runat="server">  
  protected override void Render(HtmlTextWriter writer)  
  {  
    string content = string.Empty;  
    StringWriter stringWriter = new StringWriter();  
    HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);  
    try 
    {  
      // 將當前頁面的內容呈現到臨時的 HtmlTextWriter 對象中  
      base.Render(htmlWriter);  
      htmlWriter.Close();  
      // 得到當前頁面的全部內容  
      content = stringWriter.ToString();  
      // 替換頁面中的部分內容  
      string newContent = content.Replace("[mxh]", "孟憲會");  
      // 將新頁面的內容顯示出來  
      writer.Write(newContent);  
    }  
    catch { }  
    finally 
    {  
      stringWriter.Dispose();  
      htmlWriter.Close();  
      htmlWriter.Dispose();  
    }  
  }  
</script>  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
  <title>孟憲會之替換頁面呈現內容測試</title>  
</head>  
<body>  
  <form id="form1" runat="server">  
  [mxh]  
  </form>  
</body>  
</html> 


本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/net_lover/archive/2010/02/25/5324455.aspx

發佈了11 篇原創文章 · 獲贊 11 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章