Page.CreateHtmlTextWriter(TextWriter) Method

Definition

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

protected virtual System.Web.UI.HtmlTextWriter CreateHtmlTextWriter(System.IO.TextWriter tw);
protected internal virtual System.Web.UI.HtmlTextWriter CreateHtmlTextWriter(System.IO.TextWriter tw);

Parameters

tw
TextWriter

The TextWriter used to create the HtmlTextWriter.

Returns

An HtmlTextWriter or Html32TextWriter.

Examples

The following code 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. 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>");
      }
   }
}

Remarks

The CreateHtmlTextWriter method creates a TextWriter through the Browser property of the Request object associated with the page request. You can add a reference to an HtmlTextWriter in the browserCaps configuration section. Override the CreateHtmlTextWriter method to perform custom lookup.

Applies to

Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

See also