Export (0) Print
Expand All

Html32TextWriter Class

Writes a series of HTML 3.2–specific characters and text to the output stream for an ASP.NET server control. The Html32TextWriter class provides formatting capabilities that ASP.NET server controls use when rendering HTML 3.2 content to clients.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)

[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class Html32TextWriter : HtmlTextWriter

The Html32TextWriter class is an alternative to the HtmlTextWriter class. It converts HTML 4.0 style attributes into the equivalent HTML 3.2 tags and attributes. It standardizes the propagation of attributes, such as colors and fonts, using HTML tables. ASP.NET automatically uses this class for HTML 3.2 and earlier browsers by checking the TagWriter property of the HttpBrowserCapabilities class. Unless you create a custom page or control adapter that targets devices that use HTML 3.2 markup, you do not need to create an instance of the Html32TextWriter class explicitly.

For more information about customizing page and control rendering, see Walkthrough: Developing and Using a Custom Web Server Control.

The following code example demonstrates how to use a class, named CustomHtml32TextWriter, that derives from the Html32TextWriter class. CustomHtml32TextWriter creates two constructors that follow the pattern that is established by the HtmlTextWriter class and overrides the RenderBeforeContent, RenderAfterContent, RenderBeforeTag, and RenderAfterTag methods.

using System.IO;
using System.Web.UI;

namespace Examples.AspNet
{
	public class CustomHtml32TextWriter : Html32TextWriter
	{
		// Create a constructor for the class 
		// that takes a TextWriter as a parameter. 
		public CustomHtml32TextWriter(TextWriter writer) 
			: this(writer, DefaultTabString) 
		{
		}

		// Create a constructor for the class that takes 
		// a TextWriter and a string as parameters. 
		public CustomHtml32TextWriter(TextWriter writer, String tabString) 
			: base(writer, tabString)
		{
		}
		
		// Override the RenderBeforeContent method to render 
		// styles before rendering the content of a <th> element. 
		protected override string RenderBeforeContent()
		{
			// Check the TagKey property. If its value is 
			// HtmlTextWriterTag.TH, check the value of the  
			// SupportsBold property. If true, return the 
			// opening tag of a <b> element; otherwise, render 
			// the opening tag of a <font> element with a color 
			// attribute set to the hexadecimal value for red. 
			if (TagKey == HtmlTextWriterTag.Th)
			{
				if (SupportsBold)
					return "<b>";
				else 
					return "<font color=\"FF0000\">";
			}

			// Check whether the element being rendered 
            // is an <H4> element. If it is, check the  
            // value of the SupportsItalic property. 
            // If true, render the opening tag of the <i> element 
            // prior to the <H4> element's content; otherwise,  
            // render the opening tag of a <font> element  
            // with a color attribute set to the hexadecimal 
            // value for navy blue. 
			if (TagKey == HtmlTextWriterTag.H4)
			{
				if (SupportsItalic)
					return "<i>";
				else 
					return "<font color=\"000080\">";
			}
			// Call the base method. 
			return base.RenderBeforeContent();
		}

		// Override the RenderAfterContent method to close 
		// styles opened during the call to the RenderBeforeContent 
		// method. 
		protected override string RenderAfterContent()
		{
			// Check whether the element being rendered is a <th> element. 
			// If so, and the requesting device supports bold formatting, 
			// render the closing tag of the <b> element. If not, 
			// render the closing tag of the <font> element. 
			if (TagKey == HtmlTextWriterTag.Th)
			{
				if (SupportsBold)
					return "</b>";
				else 
					return "</font>";
			}

			// Check whether the element being rendered is an <H4>. 
            // element. If so, and the requesting device supports italic 
            // formatting, render the closing tag of the <i> element. 
            // If not, render the closing tag of the <font> element. 
			if (TagKey == HtmlTextWriterTag.H4)
			{
				if (SupportsItalic)
					return "</i>";
				else 
					return "</font>";
			}
			// Call the base method 
			return base.RenderAfterContent();
		}

        // Override the RenderBeforeTag method to render the 
        // opening tag of a <small> element to modify the text size of  
        // any <a> elements that this writer encounters. 
		protected override string RenderBeforeTag()
		{
            // Check whether the element being rendered is an  
            // <a> element. If so, render the opening tag 
            // of the <small> element; otherwise, call the base method. 
			if (TagKey == HtmlTextWriterTag.A)
				return "<small>";
			return base.RenderBeforeTag();
		}

        // Override the RenderAfterTag method to render 
        // close any elements opened in the RenderBeforeTag 
        // method call. 
		protected override string RenderAfterTag()
		{
            // Check whether the element being rendered is an 
            // <a> element. If so, render the closing tag of the 
            // <small> element; otherwise, call the base method. 
			if (TagKey == HtmlTextWriterTag.A)
				return "</small>";
			return base.RenderAfterTag();
		}
	}
}

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft