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.
Assembly: System.Web (in System.Web.dll)
'Declaration <AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _ <AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _ Public Class HtmlTextWriter _ Inherits TextWriter 'Usage 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.
| Topic | Location |
|---|---|
| Developing Custom Data-Bound Web Server Controls for ASP.NET 1.1 | Authoring ASP.NET Controls |
| Walkthrough: Developing and Using a Custom Server Control | Authoring ASP.NET Controls |
| Developing Custom Data-Bound Web Server Controls for ASP.NET 2.0 | Authoring ASP.NET Controls |
| Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 2.0 | Authoring ASP.NET Controls |
| Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 1.1 | Authoring ASP.NET Controls |
| Walkthrough: Developing and Using a Custom Server Control | Authoring ASP.NET Controls |
| Developing Custom Data-Bound Web Server Controls for ASP.NET 1.1 | Authoring ASP.NET Controls |
| Developing Custom Data-Bound Web Server Controls for ASP.NET 2.0 | Authoring ASP.NET Controls |
| Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 2.0 | Authoring ASP.NET Controls |
| Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 1.1 | Authoring ASP.NET Controls |
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 "encoding " 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
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.