HtmlTextWriter クラス
アセンブリ: System.Web (system.web.dll 内)
構文
Public Class HtmlTextWriter Inherits TextWriter
public class HtmlTextWriter : TextWriter
public class HtmlTextWriter extends TextWriter
public class HtmlTextWriter extends TextWriter
HtmlTextWriter クラスは、HTML 4.0 をデスクトップ ブラウザに表示するために使用します。HtmlTextWriter は、ChtmlTextWriter クラス、Html32TextWriter クラス、XhtmlTextWriter クラスなどの System.Web.UI 名前空間のすべてのマークアップ ライタの基本クラスでもあります。これらのクラスは、さまざまな種類のマークアップの要素、属性、スタイル、およびレイアウト情報を書き込むために使用されます。さらに、これらのクラスは、各マークアップ言語に関連付けられたページとコントロール アダプタ クラスで使用されます。
ほとんどの場合、ASP.NET は、要求側のデバイスに適したライタを自動的に使用します。ただし、カスタム テキスト ライタを作成したり、特定のライタを指定して特定のデバイスにページを出力したりする場合は、アプリケーションの .browser ファイルの controlAdapters セクションで、ページにライタを割り当てる必要があります。
Control クラスから派生したカスタム コントロールの Render メソッドをオーバーライドする方法を次のコード例に示します。このコード例では、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.MarshalByRefObject
System.IO.TextWriter
System.Web.UI.HtmlTextWriter
System.Web.UI.Html32TextWriter
System.Web.UI.MobileControls.Adapters.MultiPartWriter
System.Web.UI.XhtmlTextWriter
プラットフォーム
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- HtmlTextWriter クラスのページへのリンク