Page.CreateHtmlTextWriter(TextWriter) Page.CreateHtmlTextWriter(TextWriter) Page.CreateHtmlTextWriter(TextWriter) Page.CreateHtmlTextWriter(TextWriter) Method

定義

建立 HtmlTextWriter 物件以呈現網頁內容。Creates an HtmlTextWriter object to render the page's content.

protected public:
 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 Friend Overridable Function CreateHtmlTextWriter (tw As TextWriter) As HtmlTextWriter

參數

傳回

範例

下列程式碼範例會使用CreateHtmlTextWriter方法來建立名為MyHtmlTextWriter之自訂HtmlTextWriter物件的實例。The following code example uses the CreateHtmlTextWriter method to create an instance of a custom HtmlTextWriter object named MyHtmlTextWriter. 方法會Page在衍生自的MyPage類別中覆寫,以便在要求頁面時呈現ASP.NET伺服器控制項。MyHtmlTextWriter CreateHtmlTextWriterThe CreateHtmlTextWriter method is overridden in the MyPage class, which is derived from Page, so that MyHtmlTextWriter renders ASP.NET server controls when the page is requested. 請注意,此範例會防止TextWriter介面卡行為。Note that this example will prevent adapter TextWriter behavior.

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

備註

方法會透過與頁面TextWriter要求相關聯之物件的屬性來建立。Request Browser CreateHtmlTextWriterThe CreateHtmlTextWriter method creates a TextWriter through the Browser property of the Request object associated with the page request. 您可以HtmlTextWriter在 [ browserCaps設定] 區段中新增的參考。You can add a reference to an HtmlTextWriter in the browserCaps configuration section. 覆寫CreateHtmlTextWriter方法以執行自訂查詢。Override the CreateHtmlTextWriter method to perform custom lookup.

適用於

另請參閱