Redigera

Dela via


ChtmlTextWriter Constructors

Definition

Initializes a new instance of the ChtmlTextWriter class.

Overloads

ChtmlTextWriter(TextWriter)

Initializes a new instance of the ChtmlTextWriter class that uses the DefaultTabString constant to indent lines.

ChtmlTextWriter(TextWriter, String)

Initializes a new instance of the ChtmlTextWriter class with the specified line indentation.

ChtmlTextWriter(TextWriter)

Initializes a new instance of the ChtmlTextWriter class that uses the DefaultTabString constant to indent lines.

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)

Parameters

writer
TextWriter

The TextWriter that renders the markup content.

Examples

The following code example demonstrates how to create a class named ChtmlCustomPageAdapter and defines one method, CreateCustomChtmlTextWriter, which creates and returns an instance of the CustomChtmlTextWriter class. The CustomChtmlTextWriter then renders cHTML content for pages to devices with browsers that use cHTML markup.

This code example is part of a larger example provided for the ChtmlTextWriter class.

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

Remarks

The ChtmlTextWriter class has two constructors, which is standard for all classes that derive directly or indirectly from the HtmlTextWriter class.

The ChtmlTextWriter constructor, which takes an instance of the TextWriter class as a parameter, calls the second constructor and passes it two parameter values:

  • The TextWriter.

  • The string value that is specified in the DefaultTabString field, which defines the tab spacing that is used by the XHTML text writer.

Applies to

ChtmlTextWriter(TextWriter, String)

Initializes a new instance of the ChtmlTextWriter class with the specified line indentation.

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)

Parameters

writer
TextWriter

The TextWriter that renders the markup content.

tabString
String

The number of spaces defined in the Indent.

Examples

The following code example demonstrates how to create a custom class named CustomChtmlTextWriter that is derived from the ChtmlTextWriter class. It creates two constructors that you can use to create an instance of the custom class with the same pattern as all classes that derive, directly or indirectly, from the HtmlTextWriter class.

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

Remarks

The ChtmlTextWriter constructor, which takes both an instance of the TextWriter class and a string as parameters, calls the Html32TextWriter constructor that takes the same parameters when it creates an instance of the ChtmlTextWriter class.

Applies to