ChtmlTextWriter Class
Collapse the table of content
Expand the table of content

ChtmlTextWriter Class

Writes a series of cHTML-specific characters and text to the output stream of an ASP.NET server control. The ChtmlTextWriter class provides formatting capabilities that ASP.NET server controls use when rendering cHTML content to clients.

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

[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class ChtmlTextWriter : Html32TextWriter

Compact HTML, or cHTML, is a subset of HTML 2.0, HTML 3.2, and HTML 4.0, and is designed to render on devices with limited memory and CPU power, along with small display screens, limited formatting capabilities, and a limited number of input options (such as a cell phone touch pad). Therefore, cHTML does not support the following:

  • JPEG images.

  • Tables.

  • Image maps.

  • Multiple character fonts and styles.

  • Background colors and images.

  • Frames.

  • Style sheets.

The ChtmlTextWriter class derives from the Html32TextWriter class and much of its functionality is provided by the Html32TextWriter and the base class for all markup text writers, which is the HtmlTextWriter class.

The following code example demonstrates how to create a class named CustomChtmlTextWriter that is derived from the ChtmlTextWriter class. It creates two constructors and overrides the OnAttributeRender method to prevent the bgcolor style attribute from being written to the output stream of the ChtmlTextWriter object. It also creates a class named ChtmlCustomPageAdapter that defines one method, CreateCustomChtmlTextWriter, which creates and returns an instance of the CustomChtmlTextWriter class. The CustomChtmlTextWriter then renders cHTML content for a page to devices with browsers that use cHTML markup.

// Create a class that derives from the 
// ChtmlTextWriter class. 
using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls.Adapters;

namespace AspNet.Samples.CS
	public class CustomChtmlTextWriter : ChtmlTextWriter
		// Create two constructors for the new 
		// text writer. 
		public CustomChtmlTextWriter(TextWriter writer) : base(writer, DefaultTabString)

		public CustomChtmlTextWriter(TextWriter writer, String tabString)
			: base(writer, tabString)
		// Override the OnAttributeRender method to 
		// not render the bgcolor attribute, which is 
		// not supported in CHTML. 
		protected override bool OnAttributeRender(string name, string value, HtmlTextWriterAttribute key)
			if (String.Equals("bgcolor", name))
				return false;
			// Call the ChtmlTextWriter version of the 
			// the OnAttributeRender method. 
			return base.OnAttributeRender(name, value, key);

	// Derive from the WebControlAdapter class, 
	// provide a CreateCustomChtmlTextWriter 
	// method to attach to the custom writer. 
	public class ChtmlCustomPageAdapter : WebControlAdapter
		protected internal ChtmlTextWriter CreateCustomChtmlTextWriter(
			TextWriter writer)
			return new CustomChtmlTextWriter(writer);

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

© 2015 Microsoft