Jen

動態修改page的context

override Page中 Render(HtmlTextWriter) 方法, 如下所示:

protected override void Render(HtmlTextWriter writer)
{
  System.Text.StringBuilder PageContents = new System.Text.StringBuilder();
  System.IO.StringWriter SW = new System.IO.StringWriter(PageContents);
  System.Web.UI.HtmlTextWriter HtmlWriter = new System.Web.UI.HtmlTextWriter(oSW);           
  base.Render(HtmlWriter);
  //這樣就可以利用PageContents,很輕鬆的去修改網頁內容了.

  PageContents = PageContents.Replace("@@", "some contents you want!");
  System.IO.StringReader SR = new System.IO.StringReader(PageContents.ToString());
  writer.Write(SR.ReadToEnd());
  //最後再render出去.
           
}
但是,如果在.aspx Page裏有<%Response.Write();%>的話,會有一些意想不到的結果, 很奇怪,我還在意會當中,請往下看. 在test1.aspx 中, 有一段是這樣寫的. 在test1.aspx.cs 中,改寫了Render方法.
protected override void Render(HtmlTextWriter writer)
{
  System.Text.StringBuilder PageContents = new System.Text.StringBuilder();
  System.IO.StringWriter SW = new System.IO.StringWriter(oPageContents);
  System.Web.UI.HtmlTextWriter HtmlWriter = new System.Web.UI.HtmlTextWriter(oSW);           
  base.Render(HtmlWriter);
 
  PageContents = PageContents.Replace("$Instead$", "----這是後面");
  System.IO.StringReader SR = new System.IO.StringReader(PageContents.ToString());
  writer.Write(SR.ReadToEnd());
             
}
結果跟codeproject的文章一樣, 當我再做一件事時,就是註解掉base.Render(HtmlWriter);下面的程式碼時,
protected override void Render(HtmlTextWriter writer)
{
  System.Text.StringBuilder PageContents = new System.Text.StringBuilder();
  System.IO.StringWriter SW = new System.IO.StringWriter(oPageContents);
  System.Web.UI.HtmlTextWriter HtmlWriter = new System.Web.UI.HtmlTextWriter(oSW);           
  base.Render(HtmlWriter);
 
  //PageContents = PageContents.Replace("$Instead$", "----這是後面");
  //System.IO.StringReader SR = new System.IO.StringReader(PageContents.ToString());
  //writer.Write(SR.ReadToEnd());         
}
就變成, 只有test1.aspx頁面的Response.Write();...只能說老外真的很厲害. 解決方法是,寫一個繼承StringWriter的class,然後override以下三種方法,
 public override void Write(string value) 
 public override void Write(char[] buffer, int index, int count)
 public override void Write(char value)
所以是這樣
public class HTMLStreamer: System.IO.StringWriter 
{ 
    protected System.Web.UI.HtmlTextWriter mHtmlTextWriter;
    public HTMLStreamer(System.Text.StringBuilder oStringBuilder, System.Web.UI.HtmlTextWriter innerWriter) 
        : base(oStringBuilder)
     { 
        mHtmlTextWriter = innerWriter;
     }
    
    public override void Write(string value) 
    { 
        if (value.IndexOf("$Instead$")!=-1) 
        {
            value = value.Replace("$Instead$","----這是後面"); 
        }
        mHtmlTextWriter.Write(value); base.Write(value); 
    }
    
    public override void Write(char[] buffer, int index, int count)
    { 
        mHtmlTextWriter.Write(buffer, index, count);
        base.Write(buffer, index, count);
    } 

    public override void Write(char value) 
    { 
        mHtmlTextWriter.Write(value);
        base.Write(value);
    } 
} 
然後你在test1.aspx.cs就可以如此寫,
protected override void Render(HtmlTextWriter writer) 
{ 
    System.Text.StringBuilder oPageContents = new System.Text.StringBuilder();
  
    //HTMLStreamer就是你自已寫 用來繼承StringWriter 的一個class 
    HTMLStreamer oSW = new HTMLStreamer(oPageContents, writer); 
   
    System.Web.UI.HtmlTextWriter oHtmlWriter = new System.Web.UI.HtmlTextWriter(oSW);
   
    base.Render(oHtmlWriter); 
}

我只能說阿度仔真的很厲害...

0 意見: