Skip to main content
.NET Framework Class Library
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)
Syntax
Public Class HtmlTextWriter _
	Inherits [%$TOPIC/bt7b938d_en-us_VS_110_2_0_0_0_0%]
public class HtmlTextWriter : [%$TOPIC/bt7b938d_en-us_VS_110_2_0_1_0_0%]
public ref class HtmlTextWriter : public [%$TOPIC/bt7b938d_en-us_VS_110_2_0_2_0_0%]
type HtmlTextWriter =  
    class 
        inherit [%$TOPIC/bt7b938d_en-us_VS_110_2_0_3_0_0%] 
    end

The HtmlTextWriter type exposes the following members.

Constructors
  NameDescription
Public method HtmlTextWriter(TextWriter)Initializes a new instance of the HtmlTextWriter class that uses a default tab string.
Public method HtmlTextWriter(TextWriter, String)Initializes a new instance of the HtmlTextWriter class with a specified tab string character.
Top
Properties
  NameDescription
Public property EncodingGets the encoding that the HtmlTextWriter object uses to write content to the page. (Overrides TextWriterEncoding.)
Public property FormatProviderGets an object that controls formatting. (Inherited from TextWriter.)
Public property IndentGets or sets the number of tab positions to indent the beginning of each line of markup.
Public property InnerWriterGets or sets the text writer that writes the inner content of the markup element.
Public property NewLineGets or sets the line terminator string used by the HtmlTextWriter object. (Overrides TextWriterNewLine.)
Protected property TagKeyGets or sets the HtmlTextWriterTag value for the specified markup element.
Protected property TagNameGets or sets the tag name of the markup element being rendered.
Top
Methods
  NameDescription
Public method AddAttribute(HtmlTextWriterAttribute, String)Adds the markup attribute and the attribute value to the opening tag of the element that the HtmlTextWriter object creates with a subsequent call to the RenderBeginTag method.
Public method AddAttribute(String, String)Adds the specified markup attribute and value to the opening tag of the element that the HtmlTextWriter object creates with a subsequent call to the RenderBeginTag method.
Public method AddAttribute(HtmlTextWriterAttribute, String, Boolean)Adds the markup attribute and the attribute value to the opening tag of the element that the HtmlTextWriter object creates with a subsequent call to the RenderBeginTag method, with optional encoding.
Public method AddAttribute(String, String, Boolean)Adds the specified markup attribute and value to the opening tag of the element that the HtmlTextWriter object creates with a subsequent call to the RenderBeginTag method, with optional encoding.
Protected method AddAttribute(String, String, HtmlTextWriterAttribute)Adds the specified markup attribute and value, along with an HtmlTextWriterAttribute enumeration value, to the opening tag of the element that the HtmlTextWriter object creates with a subsequent call to the RenderBeginTag method.
Public method AddStyleAttribute(HtmlTextWriterStyle, String)Adds the markup style attribute associated with the specified HtmlTextWriterStyle value and the attribute value to the opening markup tag created by a subsequent call to the RenderBeginTag method.
Public method AddStyleAttribute(String, String)Adds the specified markup style attribute and the attribute value to the opening markup tag created by a subsequent call to the RenderBeginTag method.
Protected method AddStyleAttribute(String, String, HtmlTextWriterStyle)Adds the specified markup style attribute and the attribute value, along with an HtmlTextWriterStyle enumeration value, to the opening markup tag created by a subsequent call to the RenderBeginTag method.
Public method BeginRenderNotifies an HtmlTextWriter object, or an object of a derived class, that a control is about to be rendered.
Public method CloseCloses the HtmlTextWriter object and releases any system resources associated with it. (Overrides TextWriterClose.)
Public method CreateObjRefCreates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public method DisposeReleases all resources used by the TextWriter object. (Inherited from TextWriter.)
Protected method Dispose(Boolean)Releases the unmanaged resources used by the TextWriter and optionally releases the managed resources. (Inherited from TextWriter.)
Protected method EncodeAttributeValue(HtmlTextWriterAttribute, String)Encodes the value of the specified markup attribute based on the requirements of the HttpRequest object of the current context.
Protected method EncodeAttributeValue(String, Boolean)Encodes the value of the specified markup attribute based on the requirements of the HttpRequest object of the current context.
Protected method EncodeUrlPerforms minimal URL encoding by converting spaces in the specified URL to the string "%20".
Public method EndRenderNotifies an HtmlTextWriter object, or an object of a derived class, that a control has finished rendering. You can use this method to close any markup elements opened in the BeginRender method.
Public method EnterStyle(Style)Writes the opening tag of a <span> element that contains attributes that implement the layout and character formatting of the specified style.
Public method EnterStyle(Style, HtmlTextWriterTag)Writes the opening tag of a markup element that contains attributes that implement the layout and character formatting of the specified style.
Public method Equals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public method ExitStyle(Style)Writes the closing tag of a <span> element to end the specified layout and character formatting.
Public method ExitStyle(Style, HtmlTextWriterTag)Writes the closing tag of the specified markup element to end the specified layout and character formatting.
Protected method FilterAttributesRemoves all the markup and style attributes on all properties of the page or Web server control.
Protected method FinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method FlushClears all buffers for the current HtmlTextWriter object and causes any buffered data to be written to the output stream. (Overrides TextWriterFlush.)
Public method FlushAsyncAsynchronously clears all buffers for the current writer and causes any buffered data to be written to the underlying device. (Inherited from TextWriter.)
Protected method GetAttributeKeyObtains the corresponding HtmlTextWriterAttribute enumeration value for the specified attribute.
Protected method GetAttributeNameObtains the name of the markup attribute associated with the specified HtmlTextWriterAttribute value.
Public method GetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public method GetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected method GetStyleKeyObtains the HtmlTextWriterStyle enumeration value for the specified style.
Protected method GetStyleNameObtains the markup style attribute name associated with the specified HtmlTextWriterStyle enumeration value.
Protected method GetTagKeyObtains the HtmlTextWriterTag enumeration value associated with the specified markup element.
Protected method GetTagNameObtains the markup element associated with the specified HtmlTextWriterTag enumeration value.
Public method GetTypeGets the Type of the current instance. (Inherited from Object.)
Public method InitializeLifetimeServiceObtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected method IsAttributeDefined(HtmlTextWriterAttribute)Determines whether the specified markup attribute and its value are rendered during the next call to the RenderBeginTag method.
Protected method IsAttributeDefined(HtmlTextWriterAttribute, String)Determines whether the specified markup attribute and its value are rendered during the next call to the RenderBeginTag method.
Protected method IsStyleAttributeDefined(HtmlTextWriterStyle)Determines whether the specified markup style attribute is rendered during the next call to the RenderBeginTag method.
Protected method IsStyleAttributeDefined(HtmlTextWriterStyle, String)Determines whether the specified markup style attribute and its value are rendered during the next call to the RenderBeginTag method.
Public method IsValidFormAttributeChecks an attribute to ensure that it can be rendered in the opening tag of a <form> markup element.
Protected method MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected method MemberwiseClone(Boolean)Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)
Protected method OnAttributeRenderDetermines whether the specified markup attribute and its value can be rendered to the current markup element.
Protected method OnStyleAttributeRenderDetermines whether the specified markup style attribute and its value can be rendered to the current markup element.
Protected method OnTagRenderDetermines whether the specified markup element will be rendered to the requesting page.
Protected method OutputTabsWrites a series of tab strings that represent the indentation level for a line of markup characters.
Protected method PopEndTagRemoves the most recently saved markup element from the list of rendered elements.
Protected method PushEndTagSaves the specified markup element for later use when generating the end tag for a markup element.
Protected method Static member RegisterAttributeRegisters markup attributes, whether literals or dynamically generated, from the source file so that they can be properly rendered to the requesting client.
Protected method Static member RegisterStyleRegisters markup style properties, whether literals or dynamically generated, from the source file so that they can be properly rendered to the requesting client.
Protected method Static member RegisterTagRegisters markup tags, whether literals or dynamically generated, from the source file so that they can be properly rendered to the requesting client.
Protected method RenderAfterContentWrites any text or spacing that occurs after the content and before the closing tag of the markup element to the markup output stream.
Protected method RenderAfterTagWrites any spacing or text that occurs after the closing tag for a markup element.
Protected method RenderBeforeContentWrites any text or spacing before the content and after the opening tag of a markup element.
Protected method RenderBeforeTagWrites any text or spacing that occurs before the opening tag of a markup element.
Public method RenderBeginTag(HtmlTextWriterTag)Writes the opening tag of the markup element associated with the specified HtmlTextWriterTag enumeration value to the output stream.
Public method RenderBeginTag(String)Writes the opening tag of the specified markup element to the output stream.
Public method RenderEndTagWrites the end tag of a markup element to the output stream.
Public method ToStringReturns a string that represents the current object. (Inherited from Object.)
Public method Write(Boolean)Writes the text representation of a Boolean value to the output stream, along with any pending tab spacing. (Overrides TextWriterWrite(Boolean).)
Public method Write(Char)Writes the text representation of a Unicode character to the output stream, along with any pending tab spacing. (Overrides TextWriterWrite(Char).)
Public method Write(Char)Writes the text representation of an array of Unicode characters to the output stream, along with any pending tab spacing. (Overrides TextWriterWrite(Char).)
Public method Write(Decimal)Writes the text representation of a decimal value to the text string or stream. (Inherited from TextWriter.)
Public method Write(Double)Writes the text representation of a double-precision floating-point number to the output stream, along with any pending tab spacing. (Overrides TextWriterWrite(Double).)
Public method Write(Int32)Writes the text representation of a 32-byte signed integer to the output stream, along with any pending tab spacing. (Overrides TextWriterWrite(Int32).)
Public method Write(Int64)Writes the text representation of a 64-byte signed integer to the output stream, along with any pending tab spacing. (Overrides TextWriterWrite(Int64).)
Public method Write(Object)Writes the text representation of an object to the output stream, along with any pending tab spacing. (Overrides TextWriterWrite(Object).)
Public method Write(Single)Writes the text representation of a single-precision floating-point number to the output stream, along with any pending tab spacing. (Overrides TextWriterWrite(Single).)
Public method Write(String)Writes the specified string to the output stream, along with any pending tab spacing. (Overrides TextWriterWrite(String).)
Public method Write(UInt32)Writes the text representation of a 4-byte unsigned integer to the text string or stream. (Inherited from TextWriter.)
Public method Write(UInt64)Writes the text representation of an 8-byte unsigned integer to the text string or stream. (Inherited from TextWriter.)
Public method Write(String, Object)Writes a tab string and a formatted string to the output stream, using the same semantics as the StringFormat method, along with any pending tab spacing. (Overrides TextWriterWrite(String, Object).)
Public method Write(String, Object)Writes a formatted string that contains the text representation of an object array to the output stream, along with any pending tab spacing. This method uses the same semantics as the StringFormat method. (Overrides TextWriterWrite(String, Object).)
Public method Write(Char, Int32, Int32)Writes the text representation of a subarray of Unicode characters to the output stream, along with any pending tab spacing. (Overrides TextWriterWrite(Char, Int32, Int32).)
Public method Write(String, Object, Object)Writes a formatted string that contains the text representation of two objects to the output stream, along with any pending tab spacing. This method uses the same semantics as the StringFormat method. (Overrides TextWriterWrite(String, Object, Object).)
Public method Write(String, Object, Object, Object)Writes a formatted string to the text string or stream, using the same semantics as the StringFormat(String, Object, Object, Object) method. (Inherited from TextWriter.)
Public method WriteAsync(Char)Writes a character to the text string or stream asynchronously. (Inherited from TextWriter.)
Public method WriteAsync(Char)Writes a character array to the text string or stream asynchronously. (Inherited from TextWriter.)
Public method WriteAsync(String)Writes a string to the text string or stream asynchronously. (Inherited from TextWriter.)
Public method WriteAsync(Char, Int32, Int32)Writes a subarray of characters to the text string or stream asynchronously. (Inherited from TextWriter.)
Public method WriteAttribute(String, String)Writes the specified markup attribute and value to the output stream.
Public method WriteAttribute(String, String, Boolean)Writes the specified markup attribute and value to the output stream, and, if specified, writes the value encoded.
Public method WriteBeginTagWrites any tab spacing and the opening tag of the specified markup element to the output stream.
Public method WriteBreakWrites a <br /> markup element to the output stream.
Public method WriteEncodedTextEncodes the specified text for the requesting device, and then writes it to the output stream.
Public method WriteEncodedUrlEncodes the specified URL, and then writes it to the output stream. The URL might include parameters.
Public method WriteEncodedUrlParameterEncodes the specified URL parameter for the requesting device, and then writes it to the output stream.
Public method WriteEndTagWrites any tab spacing and the closing tag of the specified markup element.
Public method WriteFullBeginTagWrites any tab spacing and the opening tag of the specified markup element to the output stream.
Public method WriteLineWrites a line terminator string to the output stream. (Overrides TextWriterWriteLine.)
Public method WriteLine(Boolean)Writes any pending tab spacing and the text representation of a Boolean value, followed by a line terminator string, to the output stream. (Overrides TextWriterWriteLine(Boolean).)
Public method WriteLine(Char)Writes any pending tab spacing and a Unicode character, followed by a line terminator string, to the output stream. (Overrides TextWriterWriteLine(Char).)
Public method WriteLine(Char)Writes any pending tab spacing and an array of Unicode characters, followed by a line terminator string, to the output stream. (Overrides TextWriterWriteLine(Char).)
Public method WriteLine(Decimal)Writes the text representation of a decimal value followed by a line terminator to the text string or stream. (Inherited from TextWriter.)
Public method WriteLine(Double)Writes any pending tab spacing and the text representation of a double-precision floating-point number, followed by a line terminator string, to the output stream. (Overrides TextWriterWriteLine(Double).)
Public method WriteLine(Int32)Writes any pending tab spacing and the text representation of a 32-byte signed integer, followed by a line terminator string, to the output stream. (Overrides TextWriterWriteLine(Int32).)
Public method WriteLine(Int64)Writes any pending tab spacing and the text representation of a 64-byte signed integer, followed by a line terminator string, to the output stream. (Overrides TextWriterWriteLine(Int64).)
Public method WriteLine(Object)Writes any pending tab spacing and the text representation of an object, followed by a line terminator string, to the output stream. (Overrides TextWriterWriteLine(Object).)
Public method WriteLine(Single)Writes any pending tab spacing and the text representation of a single-precision floating-point number, followed by a line terminator string, to the output stream. (Overrides TextWriterWriteLine(Single).)
Public method WriteLine(String)Writes any pending tab spacing and a text string, followed by a line terminator string, to the output stream. (Overrides TextWriterWriteLine(String).)
Public method WriteLine(UInt32)Writes any pending tab spacing and the text representation of a 4-byte unsigned integer, followed by a line terminator string, to the output stream. (Overrides TextWriterWriteLine(UInt32).)
Public method WriteLine(UInt64)Writes the text representation of an 8-byte unsigned integer followed by a line terminator to the text string or stream. (Inherited from TextWriter.)
Public method WriteLine(String, Object)Writes any pending tab spacing and a formatted string containing the text representation of an object, followed by a line terminator string, to the output stream. (Overrides TextWriterWriteLine(String, Object).)
Public method WriteLine(String, Object)Writes any pending tab spacing and a formatted string that contains the text representation of an object array, followed by a line terminator string, to the output stream. (Overrides TextWriterWriteLine(String, Object).)
Public method WriteLine(Char, Int32, Int32)Writes any pending tab spacing and a subarray of Unicode characters, followed by a line terminator string, to the output stream. (Overrides TextWriterWriteLine(Char, Int32, Int32).)
Public method WriteLine(String, Object, Object)Writes any pending tab spacing and a formatted string that contains the text representation of two objects, followed by a line terminator string, to the output stream. (Overrides TextWriterWriteLine(String, Object, Object).)
Public method WriteLine(String, Object, Object, Object)Writes out a formatted string and a new line, using the same semantics as Format. (Inherited from TextWriter.)
Public method WriteLineAsyncWrites a line terminator asynchronously to the text string or stream. (Inherited from TextWriter.)
Public method WriteLineAsync(Char)Writes a character followed by a line terminator asynchronously to the text string or stream. (Inherited from TextWriter.)
Public method WriteLineAsync(Char)Writes an array of characters followed by a line terminator asynchronously to the text string or stream. (Inherited from TextWriter.)
Public method WriteLineAsync(String)Writes a string followed by a line terminator asynchronously to the text string or stream. (Inherited from TextWriter.)
Public method WriteLineAsync(Char, Int32, Int32)Writes a subarray of characters followed by a line terminator asynchronously to the text string or stream. (Inherited from TextWriter.)
Public method WriteLineNoTabsWrites a string, followed by a line terminator string, to the output stream. This method ignores any specified tab spacing.
Public method WriteStyleAttribute(String, String)Writes the specified style attribute to the output stream.
Public method WriteStyleAttribute(String, String, Boolean)Writes the specified style attribute and value to the output stream, and encodes the value, if specified.
Protected method WriteUrlEncodedStringWrites the specified string, encoding it according to URL requirements.
Top
Fields
  NameDescription
Protected field CoreNewLineStores the newline characters used for this TextWriter. (Inherited from TextWriter.)
Public field Static member DefaultTabStringRepresents a single tab character.
Public field Static member DoubleQuoteCharRepresents the quotation mark (") character.
Public field Static member EndTagLeftCharsRepresents the left angle bracket and slash mark (</) of the closing tag of a markup element.
Public field Static member EqualsCharRepresents the equal sign (=).
Public field Static member EqualsDoubleQuoteStringRepresents an equal sign (=) and a double quotation mark (") together in a string (=").
Public field Static member SelfClosingCharsRepresents a space and the self-closing slash mark (/) of a markup tag.
Public field Static member SelfClosingTagEndRepresents the closing slash mark and right angle bracket (/>) of a self-closing markup element.
Public field Static member SemicolonCharRepresents the semicolon (;).
Public field Static member SingleQuoteCharRepresents an apostrophe (').
Public field Static member SlashCharRepresents the slash mark (/).
Public field Static member SpaceCharRepresents a space ( ) character.
Public field Static member StyleEqualsCharRepresents the style equals (:) character used to set style attributes equal to values.
Public field Static member TagLeftCharRepresents the opening angle bracket (<) of a markup tag.
Public field Static member TagRightCharRepresents the closing angle bracket (>) of a markup tag.
Top
Remarks

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.

Examples

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")
    writer.RenderBeginTag(HtmlTextWriterTag.Span)

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

    writer.Write("Hello")
    writer.WriteLine()

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

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

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

    writer.WriteLine()

    writer.Indent -= 1
    writer.RenderEndTag()

End Sub 'Render
// Overrides the Render method to write a <span> element 
        // that applies styles and attributes.  
        protected override void Render(HtmlTextWriter writer) 
        {     
            // 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");
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            // Create a space and indent the markup inside the  
            // <span> element.
            writer.WriteLine();
            writer.Indent++;
            writer.Write("Hello");
            writer.WriteLine();

            // Controls the encoding of markup attributes 
            // for an <img> element. Simple known values  
	        // do not need encoding.
            writer.AddAttribute(HtmlTextWriterAttribute.Alt, 
                "Encoding, \"Required\"", 
                true);
            writer.AddAttribute("myattribute", 
                "No &quot;encoding &quot; required", 
                false);
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();
            writer.WriteLine();

            // Create a non-standard markup element.
            writer.RenderBeginTag("MyTag");
            writer.Write("Contents of MyTag");
            writer.RenderEndTag();
            writer.WriteLine();

            // Create a manually rendered <img> element 
            // that contains an alt attribute.
            writer.WriteBeginTag("img");
            writer.WriteAttribute("alt", "A custom image.");
            writer.Write(HtmlTextWriter.TagRightChar);
            writer.WriteEndTag("img");
            writer.WriteLine();

            writer.Indent--;
            writer.RenderEndTag();

        }
Version Information

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0
Platforms

Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

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