Page.CreateHtmlTextWriter(TextWriter) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
建立 HtmlTextWriter 物件以呈現網頁內容。
protected:
virtual System::Web::UI::HtmlTextWriter ^ CreateHtmlTextWriter(System::IO::TextWriter ^ tw);
protected public:
virtual System::Web::UI::HtmlTextWriter ^ CreateHtmlTextWriter(System::IO::TextWriter ^ tw);
protected virtual System.Web.UI.HtmlTextWriter CreateHtmlTextWriter (System.IO.TextWriter tw);
protected internal virtual System.Web.UI.HtmlTextWriter CreateHtmlTextWriter (System.IO.TextWriter tw);
abstract member CreateHtmlTextWriter : System.IO.TextWriter -> System.Web.UI.HtmlTextWriter
override this.CreateHtmlTextWriter : System.IO.TextWriter -> System.Web.UI.HtmlTextWriter
Protected Overridable Function CreateHtmlTextWriter (tw As TextWriter) As HtmlTextWriter
Protected Friend Overridable Function CreateHtmlTextWriter (tw As TextWriter) As HtmlTextWriter
參數
- tw
- TextWriter
用來建立 TextWriter 的 HtmlTextWriter。
傳回
HtmlTextWriter 或 Html32TextWriter。
範例
下列程式碼範例會 CreateHtmlTextWriter 使用 方法來建立名為 MyHtmlTextWriter
的自訂 HtmlTextWriter 物件實例。 方法 CreateHtmlTextWriter 會在 衍生自 Page 的 MyPage
類別中覆寫,以便在 MyHtmlTextWriter
要求頁面時轉譯 ASP.NET 伺服器控制項。 請注意,此範例會防止配接 TextWriter 器行為。
namespace WebPage
{
using System;
using System.IO;
using System.Web.UI;
public class MyPage : Page
{
public MyPage():base()
{
}
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
protected override HtmlTextWriter CreateHtmlTextWriter(TextWriter writer)
{
return new MyHtmlTextWriter(writer);
}
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
protected override void Render(HtmlTextWriter writer)
{
// Write a Font control.
writer.AddAttribute("color", "red");
writer.AddAttribute("size", "6pt");
writer.RenderBeginTag(HtmlTextWriterTag.Font);
writer.Write("<br>" + "The time on the server:<br> " + System.DateTime.Now.ToLongTimeString());
writer.RenderEndTag();
}
}
public class MyHtmlTextWriter : HtmlTextWriter
{
public MyHtmlTextWriter(TextWriter writer):base(writer)
{
writer.Write("<font color=blue> 'MyHtmlTextWriter' is used for rendering.</font>");
}
}
}
Imports System.IO
Imports System.Web.UI
Namespace WebPage
Public Class MyPage
Inherits Page
Public Sub New()
MyBase.New()
End Sub
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Protected Overrides Function CreateHtmlTextWriter(ByVal writer As TextWriter) As HtmlTextWriter
Return New MyHtmlTextWriter(writer)
End Function 'CreateHtmlTextWriter
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
' Writes a Font control.
writer.AddAttribute("color", "red")
writer.AddAttribute("size", "6pt")
writer.RenderBeginTag(HtmlTextWriterTag.Font)
writer.Write(("<br>" + "The time on the server:<br> " + System.DateTime.Now.ToLongTimeString()))
writer.RenderEndTag()
End Sub
End Class
Public Class MyHtmlTextWriter
Inherits HtmlTextWriter
Public Sub New(writer As TextWriter)
MyBase.New(writer)
writer.Write("<font color=blue> 'MyHtmlTextWriter' is used for rendering.</font>")
End Sub
End Class
End Namespace 'WebPage
備註
方法 CreateHtmlTextWriter 會 TextWriter 透過 Browser 與頁面要求相關聯的 物件屬性 Request 建立 。 您可以在組態區段中新增 對 的 HtmlTextWriter browserCaps
參考。 覆寫 CreateHtmlTextWriter 方法來執行自訂查閱。