Écrit des caractères et du texte de balisage dans un flux de sortie de contrôle serveur ASP.NET. Cette classe fournit des fonctionnalités de mise en forme utilisées par les contrôles serveur ASP.NET lors du rendu du balisage sur les clients.
Espace de noms : System.Web.UI
Assembly : System.Web (dans system.web.dll)
Visual Basic (Déclaration)
Public Class HtmlTextWriter
Inherits TextWriter
Visual Basic (Utilisation)
Dim instance As HtmlTextWriter
public class HtmlTextWriter : TextWriter
public ref class HtmlTextWriter : public TextWriter
public class HtmlTextWriter extends TextWriter
public class HtmlTextWriter extends TextWriter
La classe HtmlTextWriter est utilisée pour afficher le HTML 4.0 sur les navigateurs d'ordinateurs de bureau. HtmlTextWriter est également la classe de base pour tous les writers de balisage dans l'espace de noms System.Web.UI, y compris les classes ChtmlTextWriter, Html32TextWriter et XhtmlTextWriter. Ces classes sont utilisées pour écrire les éléments, les attributs et les informations relatives aux styles et à la disposition pour les différents types de balises. De plus, ces classes sont utilisées par les classes d'adaptateur de contrôles et de page qui sont associées à chaque langage de balisage.
Dans la plupart des circonstances, ASP.NET utilise automatiquement le writer approprié pour le périphérique demandeur. Toutefois, si vous créez un TextWriter personnalisé ou si vous souhaitez spécifier un writer particulier pour restituer une page pour un périphérique spécifique, vous devez mapper le writer à la page dans la section controlAdapters du fichier browser de l'application.
L'exemple de code suivant montre comment substituer la méthode Render d'un contrôle personnalisé dérivé de la classe Control. L'exemple de code suivant montre comment utiliser les divers champs, propriétés et méthodes HtmlTextWriter.
' 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
// 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 "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--;
writer.RenderEndTag();
}
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
Les membres statiques publics (Shared en Visual Basic) de ce type sont thread-safe. Il n'est pas garanti que les membres d'instance soient thread-safe.
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition
Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.
.NET Framework
Prise en charge dans : 2.0, 1.1, 1.0