This documentation is archived and is not being maintained.

HtmlTextWriter Class

Writes markup characters and text to an ASP.NET server control output stream. This class provides formatting capabilities that ASP.NET server controls use when rendering markup to clients.

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

<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public Class HtmlTextWriter _
	Inherits TextWriter
Dim instance As HtmlTextWriter

The HtmlTextWriter class is used to render HTML 4.0 to desktop browsers. The HtmlTextWriter is also the base class for all markup writers in the System.Web.UI namespace, including the ChtmlTextWriter, Html32TextWriter, and XhtmlTextWriter classes. These classes are used to write the elements, attributes, and style and layout information for different types of markup. In addition, these classes are used by the page and control adapter classes that are associated with each markup language.

In most circumstances, ASP.NET automatically uses the appropriate writer for the requesting device. However, if you create a custom text writer or if you want to specify a particular writer to render a page for a specific device, you must map the writer to the page in the controlAdapters section of the application .browser file.

The following code example shows how to override the Render method of a custom control that is derived from the Control class. The code example illustrates how to use various HtmlTextWriter methods, properties, and fields.

' Overrides the Render method to write a <span> element 
' that applies styles and attributes.      
Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)

    ' Set attributes and values along with attributes and styles 
    ' attribute defined for a <span> element.
    writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "alert('Hello');")
    writer.AddAttribute("CustomAttribute", "CustomAttributeValue")
    writer.AddStyleAttribute(HtmlTextWriterStyle.Color, "Red")
    writer.AddStyleAttribute("CustomStyle", "CustomStyleValue")

    '  Create a space and indent the markup inside the  
    ' <span> element.
    writer.Indent += 1


    ' Controls the encoding of markup attributes 
    ' for an <img> element. Simple known values  
    ' do not need encoding.
    writer.AddAttribute(HtmlTextWriterAttribute.Alt, _
        "Encoding, ""Required""", _
    writer.AddAttribute("myattribute", _
        "No &quot;encoding &quot; required", _

    ' Create a non-standard markup element.
    writer.Write("Contents of MyTag")

    ' Create a manually rendered <img> element 
    ' that contains an alt attribute.
    writer.WriteAttribute("alt", "A custom image.")


    writer.Indent -= 1

End Sub 'Render

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, 1.1, 1.0