Export (0) Print
Expand All
Expand Minimize

Page.CreateHtmlTextWriter Method

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

Namespace: System.Web.UI
Assembly: System.Web (in system.web.dll)

protected internal virtual HtmlTextWriter CreateHtmlTextWriter (
	TextWriter tw
)
protected HtmlTextWriter CreateHtmlTextWriter (
	TextWriter tw
)
protected internal function CreateHtmlTextWriter (
	tw : TextWriter
) : HtmlTextWriter
Not applicable.

Parameters

tw

The TextWriter used to create the HtmlTextWriter.

Return Value

An HtmlTextWriter or Html32TextWriter.

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.

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>");
      }
   }

}


import System.*;  
import System.IO.*;  
import System.Web.UI.*;  
public class MyPage extends Page
{
    public MyPage()
    {
    } //MyPage

    protected HtmlTextWriter CreateHtmlTextWriter(TextWriter writer)
    {
        return new MyHtmlTextWriter(writer);
    } //CreateHtmlTextWriter

    protected 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.get_Now().ToLongTimeString());
        writer.RenderEndTag();
    } //Render
} //MyPage

public class MyHtmlTextWriter extends HtmlTextWriter
{
    public MyHtmlTextWriter(TextWriter writer)
    {
        super(writer);
        writer.Write("<font color=blue> 'MyHtmlTextWriter' is used for "
            + "rendering.</font>");
    } //MyHtmlTextWriter
} //MyHtmlTextWriter

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft