zoukankan      html  css  js  c++  java
  • 在进行 ASP.NET 开发时,有时候需要对页面输出的最终 HTML 源代码进行控制

    在进行 ASP.NET 开发时,有时候需要对页面输出的最终 HTML 源代码进行控制,是页面的 render 方法中很容易实现这个功能。下面就是一个实现的方法,注释都在代码中。

    [c-sharp] view plaincopy
     
    1. <%@ Page Language="C#" %>  
    2. <%@ Import Namespace="System.IO" %>  
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    4. <script runat="server">  
    5.   protected override void Render(HtmlTextWriter writer)  
    6.   {  
    7.     string content = string.Empty;  
    8.     StringWriter stringWriter = new StringWriter();  
    9.     HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);  
    10.     try  
    11.     {  
    12.       // 将当前页面的内容呈现到临时的 HtmlTextWriter 对象中  
    13.       base.Render(htmlWriter);  
    14.       htmlWriter.Close();  
    15.       // 得到当前页面的全部内容  
    16.       content = stringWriter.ToString();  
    17.       // 替换页面中的部分内容  
    18.       string newContent = content.Replace("[mxh]""孟宪会");  
    19.       // 将新页面的内容显示出来  
    20.       writer.Write(newContent);  
    21.     }  
    22.     catch { }  
    23.     finally  
    24.     {  
    25.       stringWriter.Dispose();  
    26.       htmlWriter.Close();  
    27.       htmlWriter.Dispose();  
    28.     }  
    29.   }  
    30. </script>  
    31. <html xmlns="http://www.w3.org/1999/xhtml">  
    32. <head id="Head1" runat="server">  
    33.   <title>孟宪会之替换页面呈现内容测试</title>  
    34. </head>  
    35. <body>  
    36.   <form id="form1" runat="server">  
    37.   [mxh]  
    38.   </form>  
    39. </body>  
    40. </html>  
  • 相关阅读:
    Spring 定时任务
    JSOUP 爬虫
    Google 翻译(中英,英中)
    linux mysql 卸载与安装及配置命令
    2.logback+slf4j+janino 配置项目的日志输出
    DW3 消息推送
    JQuery 常用知识
    intellij idea 2016.3.5 控制台取消行数限制
    1.搭建Maven 多模块应用 --Intellij IDEA 2016.3.5
    JSON 解析工具的封装(Java)
  • 原文地址:https://www.cnblogs.com/feb9903/p/3514852.html
Copyright © 2011-2022 走看看