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 中定义的空格数。

示例

下面的代码示例演示如何创建派生自 类的名为 CustomChtmlTextWriterChtmlTextWriter 自定义类。 它创建两个构造函数,可用于创建自定义类的实例,其模式与直接或间接从 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 构造函数。

适用于