Share via


HtmlTextWriter クラス

マークアップ文字とテキストを ASP.NET サーバー コントロールの出力ストリームに書き込みます。このクラスには、ASP.NET サーバー コントロールがマークアップをクライアントに表示するときに使用する書式設定機能が用意されています。

名前空間: System.Web.UI
アセンブリ: System.Web (system.web.dll 内)

構文

'宣言
Public Class HtmlTextWriter
    Inherits TextWriter
'使用
Dim instance As HtmlTextWriter
public class HtmlTextWriter : TextWriter
public ref class HtmlTextWriter : public 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 セクションで、ページにライタを割り当てる必要があります。

トピック 場所
チュートリアル : カスタム サーバー コントロールの開発と使用 ASP.NET コントロールの作成
ASP.NET 1.1 用カスタム データ バインド Web サーバー コントロールの開発 ASP.NET コントロールの作成
ASP.NET 2.0 用カスタム データ バインド Web サーバー コントロールの開発 ASP.NET コントロールの作成
チュートリアル : ASP.NET 2.0 用カスタム データ バインド ASP.NET Web コントロールの作成 ASP.NET コントロールの作成
チュートリアル : ASP.NET 1.1 用カスタム データ バインド ASP.NET Web コントロールの作成 ASP.NET コントロールの作成
チュートリアル : カスタム サーバー コントロールの開発と使用 Visual Web Developer でのアプリケーションの作成
ASP.NET 1.1 用カスタム データ バインド Web サーバー コントロールの開発 Visual Studio ASP .NET での Web アプリケーションの作成
チュートリアル : ASP.NET 1.1 用カスタム データ バインド ASP.NET Web コントロールの作成 Visual Studio ASP .NET での Web アプリケーションの作成
ASP.NET 2.0 用カスタム データ バインド Web サーバー コントロールの開発 Visual Studio ASP .NET での Web アプリケーションの作成
チュートリアル : ASP.NET 2.0 用カスタム データ バインド ASP.NET Web コントロールの作成 Visual Studio ASP .NET での Web アプリケーションの作成
ASP.NET 1.1 用カスタム データ バインド Web サーバー コントロールの開発 Visual Studio ASP .NET での Web アプリケーションの作成
ASP.NET 2.0 用カスタム データ バインド Web サーバー コントロールの開発 Visual Studio ASP .NET での Web アプリケーションの作成
チュートリアル : ASP.NET 2.0 用カスタム データ バインド ASP.NET Web コントロールの作成 Visual Studio ASP .NET での Web アプリケーションの作成
チュートリアル : ASP.NET 1.1 用カスタム データ バインド ASP.NET Web コントロールの作成 Visual Studio ASP .NET での Web アプリケーションの作成

使用例

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 &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();

}

継承階層

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

スレッド セーフ

この型の public static (Visual Basicでは共有) メンバはすべて,スレッド セーフです。インスタンス メンバの場合は,スレッド セーフであるとは限りません。

プラットフォーム

Windows 98,Windows Server 2000 SP4,Windows CE,Windows Millennium Edition,Windows Mobile for Pocket PC,Windows Mobile for Smartphone,Windows Server 2003,Windows XP Media Center Edition,Windows XP Professional x64 Edition,Windows XP SP2,Windows XP Starter Edition

Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。

バージョン情報

.NET Framework

サポート対象 : 3.0,2.0,1.1,1.0

参照

関連項目

HtmlTextWriter メンバ
System.Web.UI 名前空間
TextWriter
Control
Page
ControlAdapter
PageAdapter
ChtmlTextWriter
Html32TextWriter
XhtmlTextWriter
Control.Render