Compartilhar via


ChtmlTextWriter Construtores

Definição

Inicializa uma nova instância da classe ChtmlTextWriter.

Sobrecargas

ChtmlTextWriter(TextWriter)

Inicializa uma nova instância da classe ChtmlTextWriter que usa a constante DefaultTabString para recuar linhas.

ChtmlTextWriter(TextWriter, String)

Inicializa uma nova instância da classe ChtmlTextWriter com o recuo de linha especificado.

ChtmlTextWriter(TextWriter)

Inicializa uma nova instância da classe ChtmlTextWriter que usa a constante DefaultTabString para recuar linhas.

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)

Parâmetros

writer
TextWriter

O TextWriter que renderiza o conteúdo de marcação.

Exemplos

O exemplo de código a seguir demonstra como criar uma classe chamada ChtmlCustomPageAdapter e define um método, CreateCustomChtmlTextWriter, que cria e retorna uma instância da CustomChtmlTextWriter classe . Em CustomChtmlTextWriter seguida, o renderiza o conteúdo cHTML para páginas em dispositivos com navegadores que usam marcação cHTML.

Este exemplo de código faz parte de um exemplo maior fornecido para a ChtmlTextWriter classe .

// 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

Comentários

A ChtmlTextWriter classe tem dois construtores, que é padrão para todas as classes que derivam direta ou indiretamente da HtmlTextWriter classe .

O ChtmlTextWriter construtor, que usa uma instância da TextWriter classe como um parâmetro, chama o segundo construtor e passa dois valores de parâmetro:

  • O TextWriter.

  • O valor da cadeia de caracteres especificado no DefaultTabString campo , que define o espaçamento de guia usado pelo gravador de texto XHTML.

Aplica-se a

ChtmlTextWriter(TextWriter, String)

Inicializa uma nova instância da classe ChtmlTextWriter com o recuo de linha especificado.

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)

Parâmetros

writer
TextWriter

O TextWriter que renderiza o conteúdo de marcação.

tabString
String

O número de espaços definidos no Indent.

Exemplos

O exemplo de código a seguir demonstra como criar uma classe personalizada chamada CustomChtmlTextWriter derivada da ChtmlTextWriter classe . Ele cria dois construtores que você pode usar para criar uma instância da classe personalizada com o mesmo padrão que todas as classes que derivam, direta ou indiretamente, da HtmlTextWriter classe .

// 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

Comentários

O ChtmlTextWriter construtor, que usa uma instância da TextWriter classe e uma cadeia de caracteres como parâmetros, chama o Html32TextWriter construtor que usa os mesmos parâmetros quando cria uma instância da ChtmlTextWriter classe .

Aplica-se a