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

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);
    }
}

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

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

ChtmlTextWriter(TextWriter, String)

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

public ChtmlTextWriter(System.IO.TextWriter writer, string tabString);

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);
        }
    }
}

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

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1