共用方式為


ChtmlTextWriter 建構函式

定義

初始化 ChtmlTextWriter 類別的新執行個體。

多載

ChtmlTextWriter(TextWriter)

使用縮排行的 ChtmlTextWriter 常數,初始化 DefaultTabString 類別的新執行個體。

ChtmlTextWriter(TextWriter, String)

使用指定的行縮排,初始化 ChtmlTextWriter 類別的新執行個體。

ChtmlTextWriter(TextWriter)

使用縮排行的 ChtmlTextWriter 常數,初始化 DefaultTabString 類別的新執行個體。

public:
 ChtmlTextWriter(System::IO::TextWriter ^ writer);
public ChtmlTextWriter (System.IO.TextWriter writer);
new System.Web.UI.ChtmlTextWriter : System.IO.TextWriter -> System.Web.UI.ChtmlTextWriter
Public Sub New (writer As TextWriter)

參數

writer
TextWriter

呈現標記內容的 TextWriter

範例

下列程式碼範例示範如何建立名為 ChtmlCustomPageAdapter 的類別,並定義一個方法, CreateCustomChtmlTextWriter 這個方法會建立並傳回 類別的 CustomChtmlTextWriter 實例。 接著,會將 CustomChtmlTextWriter 頁面的 cHTML 內容轉譯為具有使用 cHTML 標籤之瀏覽器的裝置。

此程式碼範例是針對 類別提供的較大範例的 ChtmlTextWriter 一部分。

// Derive from the WebControlAdapter class,
// provide a CreateCustomChtmlTextWriter
// method to attach to the custom writer.
public class ChtmlCustomPageAdapter : WebControlAdapter
{
    protected internal ChtmlTextWriter CreateCustomChtmlTextWriter(
        TextWriter writer)
    {
        return new CustomChtmlTextWriter(writer);
    }
}
' Derive from the WebControlAdapter class,
' provide a CreateCustomChtmlTextWriter
' method to attach the custom writer.
Public Class ChtmlCustomPageAdapter
    Inherits WebControlAdapter

    Protected Friend Function CreateCustomChtmlTextWriter( _
     ByVal writer As TextWriter) As ChtmlTextWriter

        Return New CustomChtmlTextWriter(writer)

    End Function
End Class

備註

類別 ChtmlTextWriter 有兩個建構函式,這是直接或間接衍生自 類別的所有類別的標準 HtmlTextWriter

採用 ChtmlTextWriter 類別實例做為參數的 TextWriter 建構函式會呼叫第二個建構函式,並傳遞兩個參數值:

適用於

ChtmlTextWriter(TextWriter, String)

使用指定的行縮排,初始化 ChtmlTextWriter 類別的新執行個體。

public:
 ChtmlTextWriter(System::IO::TextWriter ^ writer, System::String ^ tabString);
public ChtmlTextWriter (System.IO.TextWriter writer, string tabString);
new System.Web.UI.ChtmlTextWriter : System.IO.TextWriter * string -> System.Web.UI.ChtmlTextWriter
Public Sub New (writer As TextWriter, tabString As String)

參數

writer
TextWriter

呈現標記內容的 TextWriter

tabString
String

Indent 中定義的空格數目。

範例

下列程式碼範例示範如何建立衍生自 ChtmlTextWriter 類別的 CustomChtmlTextWriter 自訂類別。 它會建立兩個建構函式,讓您用來建立自訂類別的實例,其模式與類別 HtmlTextWriter 中衍生、直接或間接衍生的所有類別相同。

// Create a class that derives from the
// ChtmlTextWriter class.
using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls.Adapters;

namespace AspNet.Samples.CS
{
    public class CustomChtmlTextWriter : ChtmlTextWriter
    {
        // Create two constructors for the new
        // text writer.
        public CustomChtmlTextWriter(TextWriter writer) : base(writer, DefaultTabString)
        {
        }

        public CustomChtmlTextWriter(TextWriter writer, String tabString)
            : base(writer, tabString)
        {
        }
        
        // Override the OnAttributeRender method to
        // not render the bgcolor attribute, which is
        // not supported in CHTML.
        protected override bool OnAttributeRender(string name, string value, HtmlTextWriterAttribute key)
        {
            if (String.Equals("bgcolor", name))
            {
                return false;
            }
            
            // Call the ChtmlTextWriter version of the
            // the OnAttributeRender method.
            return base.OnAttributeRender(name, value, key);
        }
    }

    // Derive from the WebControlAdapter class,
    // provide a CreateCustomChtmlTextWriter
    // method to attach to the custom writer.
    public class ChtmlCustomPageAdapter : WebControlAdapter
    {
        protected internal ChtmlTextWriter CreateCustomChtmlTextWriter(
            TextWriter writer)
        {
            return new CustomChtmlTextWriter(writer);
        }
    }
}
' Create a class that derives from the
' ChtmlTextWriter class.
Imports System.IO
Imports System.Web.UI
Imports System.Web.UI.WebControls.Adapters

Namespace AspNet.Samples.VB

    Public Class CustomChtmlTextWriter
        Inherits ChtmlTextWriter

        ' Create two constructors for the new
        ' text writer.
        Public Sub New(ByVal writer As TextWriter)
            MyClass.New(writer, DefaultTabString)
        End Sub

        Public Sub New(ByVal writer As TextWriter, ByVal tabString As String)
            MyBase.New(writer, tabString)
        End Sub

        ' Override the OnAttributeRender method to
        ' not render the bgcolor attribute, which is 
        ' not supported in CHTML.
        Protected Overrides Function OnAttributeRender(ByVal name As String, ByVal value As String, ByVal key As HtmlTextWriterAttribute) As Boolean
            If (String.Equals("bgcolor", name)) Then
                Return False
            End If

            ' Call the ChtmlTextWriter version of 
            ' the OnAttributeRender method.
            MyBase.OnAttributeRender(name, value, key)

        End Function
    End Class

    ' Derive from the WebControlAdapter class,
    ' provide a CreateCustomChtmlTextWriter
    ' method to attach the custom writer.
    Public Class ChtmlCustomPageAdapter
        Inherits WebControlAdapter

        Protected Friend Function CreateCustomChtmlTextWriter( _
         ByVal writer As TextWriter) As ChtmlTextWriter

            Return New CustomChtmlTextWriter(writer)

        End Function
    End Class
End Namespace

備註

ChtmlTextWriter 構函式會同時接受 類別的 TextWriter 實例和字串做為參數,它會呼叫 Html32TextWriter 建構函式,在建立 類別的 ChtmlTextWriter 實例時採用相同的參數。

適用於