Export (0) Print
Expand All

Page.CreateHtmlTextWriter Method

Creates an HtmlTextWriter object to render the page's content.

[Visual Basic]
Protected Overridable Function CreateHtmlTextWriter( _
   ByVal tw As TextWriter _
) As HtmlTextWriter
[C#]
protected virtual HtmlTextWriter CreateHtmlTextWriter(
 TextWriter tw
);
[C++]
protected: virtual HtmlTextWriter* CreateHtmlTextWriter(
 TextWriter* tw
);
[JScript]
protected function CreateHtmlTextWriter(
   tw : TextWriter
) : HtmlTextWriter;

Parameters

tw
The text writer to create.

Return Value

The specified HtmlTextWriter or Html32TextWriter object.

Remarks

The object created is based on information from the HttpBrowserCapabilities object associated with the page request. Usually, this object is an HtmlTextWriter or Html32TextWriter object. For derived pages, you can override this method to create a custom text writer.

Example

[Visual Basic, C#, C++] The following example uses the CreateHtmlTextWriter method to create an instance of a custom HtmlTextWriter object named MyHtmlTextWriter. The 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.

[Visual Basic] 
Imports System
Imports System.IO
Imports System.Web.UI

Namespace WebPage

    
   Public Class MyPage
      Inherits Page
      
      Public Sub New()
         MyBase.New()
      End Sub 'New

      <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 'Render
   End Class 'MyPage
    

   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 'New
   End Class 'MyHtmlTextWriter
End Namespace 'WebPage

[C#] 
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>");
      }
   }

}


[C++] 
#using <mscorlib.dll>
#using <System.Web.dll>
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Web::UI;

public __gc class MyHtmlTextWriter : public HtmlTextWriter
{
public:
   MyHtmlTextWriter(TextWriter* writer): HtmlTextWriter(writer)
   {
      writer->Write(S"<font color=blue> 'MyHtmlTextWriter' is used for rendering.</font>");
   }
};

public __gc class MyPage : public Page
{
public:
   MyPage():Page()
   {
   }

protected:
   [System::Security::Permissions::PermissionSet(System::Security::Permissions::SecurityAction::Demand, Name=S"FullTrust")] 
   HtmlTextWriter* CreateHtmlTextWriter(TextWriter* writer)
   {
      return new MyHtmlTextWriter(writer);
   }

   [System::Security::Permissions::PermissionSet(System::Security::Permissions::SecurityAction::Demand, Name=S"FullTrust")] 
   void Render(HtmlTextWriter* writer)
   {         
      // Write a Font control.
      writer->AddAttribute(S"color", S"red");
      writer->AddAttribute(S"size", S"6pt");
      writer->RenderBeginTag(HtmlTextWriterTag::Font);
      writer->Write(S"<br>The time on the server:<br> {0}", System::DateTime::Now.ToLongTimeString());
      writer->RenderEndTag();
   }
};

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

Page Class | Page Members | System.Web.UI Namespace | HtmlTextWriter | Html32TextWriter | HttpBrowserCapabilities

Show:
© 2014 Microsoft