System.Web.UI Namespace


.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

Visual Basic (Declaration)
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public Class HtmlTextWriter _
    Inherits TextWriter
Visual Basic (Usage)
Dim instance As HtmlTextWriter
C#
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class HtmlTextWriter : TextWriter
Visual C++
[AspNetHostingPermissionAttribute(SecurityAction::InheritanceDemand, Level = AspNetHostingPermissionLevel::Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction::LinkDemand, Level = AspNetHostingPermissionLevel::Minimal)]
public ref class HtmlTextWriter : public TextWriter
JScript
public class HtmlTextWriter extends TextWriter
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.

TopicLocation
Developing Custom Data-Bound Controls for ASP.NET 1.1Authoring ASP.NET Controls
Developing Custom Data-Bound Controls for ASP.NET 1.1Authoring ASP.NET Controls
Developing Custom Data-Bound Controls for ASP.NET 1.1Building ASP .NET Web Applications in Visual Studio
Developing Custom Data-Bound Controls for ASP.NET 1.1Building ASP .NET Web Applications in Visual Studio
Developing Custom Data-Bound Controls for ASP.NET 2.0Authoring ASP.NET Controls
Developing Custom Data-Bound Controls for ASP.NET 2.0Authoring ASP.NET Controls
Developing Custom Data-Bound Controls for ASP.NET 2.0 and laterBuilding ASP .NET Web Applications in Visual Studio
Developing Custom Data-Bound Controls for ASP.NET 2.0 and laterBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Creating a Custom Data-Bound ASP.NET Web ControlBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 1.1Authoring ASP.NET Controls
Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 1.1Authoring ASP.NET Controls
Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 1.1Building ASP .NET Web Applications in Visual Studio
Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 1.1Building ASP .NET Web Applications in Visual Studio
Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 2.0Authoring ASP.NET Controls
Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 2.0Authoring ASP.NET Controls
Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 2.0Building ASP .NET Web Applications in Visual Studio
Walkthrough: Developing and Using a Custom Server ControlAuthoring ASP.NET Controls
Walkthrough: Developing and Using a Custom Server ControlAuthoring ASP.NET Controls
Walkthrough: Developing and Using a Custom Server ControlBuilding Applications with Visual Web Developer
Walkthrough: Developing and Using a Custom Server ControlBuilding ASP .NET Web Applications in Visual Studio
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.

Visual Basic
' 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
C#
        // 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();

        }
Inheritance Hierarchy

System..::.Object
  System..::.MarshalByRefObject
    System.IO..::.TextWriter
      System.Web.UI..::.HtmlTextWriter
        System.Web.UI..::.Html32TextWriter
        System.Web.UI.MobileControls.Adapters..::.MultiPartWriter
        System.Web.UI..::.XhtmlTextWriter
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.
Platforms

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.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Tags :


Page view tracker