Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

XhtmlTextWriter Class

Writes Extensible Hypertext Markup Language (XHTML)-specific characters, including all variations of XHTML modules that derive from XTHML, to the output stream for an ASP.NET server control for mobile devices. Override the XhtmlTextWriter class to provide custom XHTML rendering for ASP.NET pages and server controls.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)
public class XhtmlTextWriter : HtmlTextWriter

The XhtmlTextWriter type exposes the following members.

  NameDescription
Public methodXhtmlTextWriter(TextWriter)Initializes a new instance of the XhtmlTextWriter class that uses the line indentation that is specified in the DefaultTabString field. Use the XhtmlTextWriter constructor if you do not want to change the default line indentation.
Public methodXhtmlTextWriter(TextWriter, String)Initializes a new instance of the XhtmlTextWriter class with the specified line indentation.
Top
  NameDescription
Protected propertyCommonAttributesGets a Hashtable object containing common attributes of the markup tags for the XhtmlTextWriter object.
Protected propertyElementSpecificAttributesGets a Hashtable object containing element-specific attributes.
Public propertyEncodingGets the encoding that the HtmlTextWriter object uses to write content to the page. (Inherited from HtmlTextWriter.)
Public propertyFormatProviderGets an object that controls formatting. (Inherited from TextWriter.)
Public propertyIndentGets or sets the number of tab positions to indent the beginning of each line of markup. (Inherited from HtmlTextWriter.)
Public propertyInnerWriterGets or sets the text writer that writes the inner content of the markup element. (Inherited from HtmlTextWriter.)
Public propertyNewLineGets or sets the line terminator string used by the HtmlTextWriter object. (Inherited from HtmlTextWriter.)
Protected propertySuppressCommonAttributesGets a Hashtable object of elements for which CommonAttributes attributes are suppressed.
Protected propertyTagKeyGets or sets the HtmlTextWriterTag value for the specified markup element. (Inherited from HtmlTextWriter.)
Protected propertyTagNameGets or sets the tag name of the markup element being rendered. (Inherited from HtmlTextWriter.)
Top
  NameDescription
Public methodAddAttribute(HtmlTextWriterAttribute, String)Adds the markup attribute and the attribute value to the opening tag of the element that the HtmlTextWriter object creates with a subsequent call to the RenderBeginTag method. (Inherited from HtmlTextWriter.)
Public methodAddAttribute(String, String)Adds the specified markup attribute and value to the opening tag of the element that the HtmlTextWriter object creates with a subsequent call to the RenderBeginTag method. (Inherited from HtmlTextWriter.)
Public methodAddAttribute(HtmlTextWriterAttribute, String, Boolean)Adds the markup attribute and the attribute value to the opening tag of the element that the HtmlTextWriter object creates with a subsequent call to the RenderBeginTag method, with optional encoding. (Inherited from HtmlTextWriter.)
Public methodAddAttribute(String, String, Boolean)Adds the specified markup attribute and value to the opening tag of the element that the HtmlTextWriter object creates with a subsequent call to the RenderBeginTag method, with optional encoding. (Inherited from HtmlTextWriter.)
Protected methodAddAttribute(String, String, HtmlTextWriterAttribute)Adds the specified markup attribute and value, along with an HtmlTextWriterAttribute enumeration value, to the opening tag of the element that the HtmlTextWriter object creates with a subsequent call to the RenderBeginTag method. (Inherited from HtmlTextWriter.)
Public methodAddRecognizedAttributeAdds an attribute to an XHTML element. The collection of element-specific attributes for the XhtmlTextWriter object is referenced by the ElementSpecificAttributes property.
Public methodAddStyleAttribute(HtmlTextWriterStyle, String)Adds the markup style attribute associated with the specified HtmlTextWriterStyle value and the attribute value to the opening markup tag created by a subsequent call to the RenderBeginTag method. (Inherited from HtmlTextWriter.)
Public methodAddStyleAttribute(String, String)Adds the specified markup style attribute and the attribute value to the opening markup tag created by a subsequent call to the RenderBeginTag method. (Inherited from HtmlTextWriter.)
Protected methodAddStyleAttribute(String, String, HtmlTextWriterStyle)Adds the specified markup style attribute and the attribute value, along with an HtmlTextWriterStyle enumeration value, to the opening markup tag created by a subsequent call to the RenderBeginTag method. (Inherited from HtmlTextWriter.)
Public methodBeginRenderNotifies an HtmlTextWriter object, or an object of a derived class, that a control is about to be rendered. (Inherited from HtmlTextWriter.)
Public methodCloseCloses the HtmlTextWriter object and releases any system resources associated with it. (Inherited from HtmlTextWriter.)
Public methodCreateObjRefCreates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public methodDispose()Releases all resources used by the TextWriter object. (Inherited from TextWriter.)
Protected methodDispose(Boolean)Releases the unmanaged resources used by the TextWriter and optionally releases the managed resources. (Inherited from TextWriter.)
Protected methodEncodeAttributeValue(HtmlTextWriterAttribute, String)Encodes the value of the specified markup attribute based on the requirements of the HttpRequest object of the current context. (Inherited from HtmlTextWriter.)
Protected methodEncodeAttributeValue(String, Boolean)Encodes the value of the specified markup attribute based on the requirements of the HttpRequest object of the current context. (Inherited from HtmlTextWriter.)
Protected methodEncodeUrlPerforms minimal URL encoding by converting spaces in the specified URL to the string "%20". (Inherited from HtmlTextWriter.)
Public methodEndRenderNotifies an HtmlTextWriter object, or an object of a derived class, that a control has finished rendering. You can use this method to close any markup elements opened in the BeginRender method. (Inherited from HtmlTextWriter.)
Public methodEnterStyle(Style)Writes the opening tag of a <span> element that contains attributes that implement the layout and character formatting of the specified style. (Inherited from HtmlTextWriter.)
Public methodEnterStyle(Style, HtmlTextWriterTag)Writes the opening tag of a markup element that contains attributes that implement the layout and character formatting of the specified style. (Inherited from HtmlTextWriter.)
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodExitStyle(Style)Writes the closing tag of a <span> element to end the specified layout and character formatting. (Inherited from HtmlTextWriter.)
Public methodExitStyle(Style, HtmlTextWriterTag)Writes the closing tag of the specified markup element to end the specified layout and character formatting. (Inherited from HtmlTextWriter.)
Protected methodFilterAttributesRemoves all the markup and style attributes on all properties of the page or Web server control. (Inherited from HtmlTextWriter.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodFlushClears all buffers for the current HtmlTextWriter object and causes any buffered data to be written to the output stream. (Inherited from HtmlTextWriter.)
Public methodFlushAsyncAsynchronously clears all buffers for the current writer and causes any buffered data to be written to the underlying device. (Inherited from TextWriter.)
Protected methodGetAttributeKeyObtains the corresponding HtmlTextWriterAttribute enumeration value for the specified attribute. (Inherited from HtmlTextWriter.)
Protected methodGetAttributeNameObtains the name of the markup attribute associated with the specified HtmlTextWriterAttribute value. (Inherited from HtmlTextWriter.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected methodGetStyleKeyObtains the HtmlTextWriterStyle enumeration value for the specified style. (Inherited from HtmlTextWriter.)
Protected methodGetStyleNameObtains the markup style attribute name associated with the specified HtmlTextWriterStyle enumeration value. (Inherited from HtmlTextWriter.)
Protected methodGetTagKeyObtains the HtmlTextWriterTag enumeration value associated with the specified markup element. (Inherited from HtmlTextWriter.)
Protected methodGetTagNameObtains the markup element associated with the specified HtmlTextWriterTag enumeration value. (Inherited from HtmlTextWriter.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInitializeLifetimeServiceObtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected methodIsAttributeDefined(HtmlTextWriterAttribute)Determines whether the specified markup attribute and its value are rendered during the next call to the RenderBeginTag method. (Inherited from HtmlTextWriter.)
Protected methodIsAttributeDefined(HtmlTextWriterAttribute, String)Determines whether the specified markup attribute and its value are rendered during the next call to the RenderBeginTag method. (Inherited from HtmlTextWriter.)
Protected methodIsStyleAttributeDefined(HtmlTextWriterStyle)Determines whether the specified markup style attribute is rendered during the next call to the RenderBeginTag method. (Inherited from HtmlTextWriter.)
Protected methodIsStyleAttributeDefined(HtmlTextWriterStyle, String)Determines whether the specified markup style attribute and its value are rendered during the next call to the RenderBeginTag method. (Inherited from HtmlTextWriter.)
Public methodIsValidFormAttributeChecks an XHTML attribute to ensure that it can be rendered in the opening tag of a <form> element. (Overrides HtmlTextWriter.IsValidFormAttribute(String).)
Protected methodMemberwiseClone()Creates a shallow copy of the current Object. (Inherited from Object.)
Protected methodMemberwiseClone(Boolean)Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)
Protected methodOnAttributeRenderDetermines whether the specified XHTML attribute and its value can be rendered to the current markup element. (Overrides HtmlTextWriter.OnAttributeRender(String, String, HtmlTextWriterAttribute).)
Protected methodOnStyleAttributeRenderDetermines whether the specified XHTML style attribute and its value can be rendered to the current markup element. (Overrides HtmlTextWriter.OnStyleAttributeRender(String, String, HtmlTextWriterStyle).)
Protected methodOnTagRenderDetermines whether the specified markup element will be rendered to the requesting page. (Inherited from HtmlTextWriter.)
Protected methodOutputTabsWrites a series of tab strings that represent the indentation level for a line of markup characters. (Inherited from HtmlTextWriter.)
Protected methodPopEndTagRemoves the most recently saved markup element from the list of rendered elements. (Inherited from HtmlTextWriter.)
Protected methodPushEndTagSaves the specified markup element for later use when generating the end tag for a markup element. (Inherited from HtmlTextWriter.)
Public methodRemoveRecognizedAttributeRemoves an attribute from the ElementSpecificAttributes collection of an element.
Protected methodRenderAfterContentWrites any text or spacing that occurs after the content and before the closing tag of the markup element to the markup output stream. (Inherited from HtmlTextWriter.)
Protected methodRenderAfterTagWrites any spacing or text that occurs after the closing tag for a markup element. (Inherited from HtmlTextWriter.)
Protected methodRenderBeforeContentWrites any text or spacing before the content and after the opening tag of a markup element. (Inherited from HtmlTextWriter.)
Protected methodRenderBeforeTagWrites any text or spacing that occurs before the opening tag of a markup element. (Inherited from HtmlTextWriter.)
Public methodRenderBeginTag(HtmlTextWriterTag)Writes the opening tag of the markup element associated with the specified HtmlTextWriterTag enumeration value to the output stream. (Inherited from HtmlTextWriter.)
Public methodRenderBeginTag(String)Writes the opening tag of the specified markup element to the output stream. (Inherited from HtmlTextWriter.)
Public methodRenderEndTagWrites the end tag of a markup element to the output stream. (Inherited from HtmlTextWriter.)
Public methodSetDocTypeSpecifies the XHTML document type for the text writer to render to the page or control.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodWrite(Boolean)Writes the text representation of a Boolean value to the output stream, along with any pending tab spacing. (Inherited from HtmlTextWriter.)
Public methodWrite(Char)Writes the text representation of a Unicode character to the output stream, along with any pending tab spacing. (Inherited from HtmlTextWriter.)
Public methodWrite(Char[])Writes the text representation of an array of Unicode characters to the output stream, along with any pending tab spacing. (Inherited from HtmlTextWriter.)
Public methodWrite(Decimal)Writes the text representation of a decimal value to the text string or stream. (Inherited from TextWriter.)
Public methodWrite(Double)Writes the text representation of a double-precision floating-point number to the output stream, along with any pending tab spacing. (Inherited from HtmlTextWriter.)
Public methodWrite(Int32)Writes the text representation of a 32-byte signed integer to the output stream, along with any pending tab spacing. (Inherited from HtmlTextWriter.)
Public methodWrite(Int64)Writes the text representation of a 64-byte signed integer to the output stream, along with any pending tab spacing. (Inherited from HtmlTextWriter.)
Public methodWrite(Object)Writes the text representation of an object to the output stream, along with any pending tab spacing. (Inherited from HtmlTextWriter.)
Public methodWrite(Single)Writes the text representation of a single-precision floating-point number to the output stream, along with any pending tab spacing. (Inherited from HtmlTextWriter.)
Public methodWrite(String)Writes the specified string to the output stream, along with any pending tab spacing. (Inherited from HtmlTextWriter.)
Public methodWrite(UInt32)Writes the text representation of a 4-byte unsigned integer to the text string or stream. (Inherited from TextWriter.)
Public methodWrite(UInt64)Writes the text representation of an 8-byte unsigned integer to the text string or stream. (Inherited from TextWriter.)
Public methodWrite(String, Object)Writes a tab string and a formatted string to the output stream, using the same semantics as the String.Format method, along with any pending tab spacing. (Inherited from HtmlTextWriter.)
Public methodWrite(String, Object[])Writes a formatted string that contains the text representation of an object array to the output stream, along with any pending tab spacing. This method uses the same semantics as the String.Format method. (Inherited from HtmlTextWriter.)
Public methodWrite(Char[], Int32, Int32)Writes the text representation of a subarray of Unicode characters to the output stream, along with any pending tab spacing. (Inherited from HtmlTextWriter.)
Public methodWrite(String, Object, Object)Writes a formatted string that contains the text representation of two objects to the output stream, along with any pending tab spacing. This method uses the same semantics as the String.Format method. (Inherited from HtmlTextWriter.)
Public methodWrite(String, Object, Object, Object)Writes a formatted string to the text string or stream, using the same semantics as the String.Format(String, Object, Object, Object) method. (Inherited from TextWriter.)
Public methodWriteAsync(Char)Writes a character to the text string or stream asynchronously. (Inherited from TextWriter.)
Public methodWriteAsync(Char[])Writes a character array to the text string or stream asynchronously. (Inherited from TextWriter.)
Public methodWriteAsync(String)Writes a string to the text string or stream asynchronously. (Inherited from TextWriter.)
Public methodWriteAsync(Char[], Int32, Int32)Writes a subarray of characters to the text string or stream asynchronously. (Inherited from TextWriter.)
Public methodWriteAttribute(String, String)Writes the specified markup attribute and value to the output stream. (Inherited from HtmlTextWriter.)
Public methodWriteAttribute(String, String, Boolean)Writes the specified markup attribute and value to the output stream, and, if specified, writes the value encoded. (Inherited from HtmlTextWriter.)
Public methodWriteBeginTagWrites any tab spacing and the opening tag of the specified markup element to the output stream. (Inherited from HtmlTextWriter.)
Public methodWriteBreakWrites a <br/> element to the XHTML output stream. (Overrides HtmlTextWriter.WriteBreak().)
Public methodWriteEncodedTextEncodes the specified text for the requesting device, and then writes it to the output stream. (Inherited from HtmlTextWriter.)
Public methodWriteEncodedUrlEncodes the specified URL, and then writes it to the output stream. The URL might include parameters. (Inherited from HtmlTextWriter.)
Public methodWriteEncodedUrlParameterEncodes the specified URL parameter for the requesting device, and then writes it to the output stream. (Inherited from HtmlTextWriter.)
Public methodWriteEndTagWrites any tab spacing and the closing tag of the specified markup element. (Inherited from HtmlTextWriter.)
Public methodWriteFullBeginTagWrites any tab spacing and the opening tag of the specified markup element to the output stream. (Inherited from HtmlTextWriter.)
Public methodWriteLine()Writes a line terminator string to the output stream. (Inherited from HtmlTextWriter.)
Public methodWriteLine(Boolean)Writes any pending tab spacing and the text representation of a Boolean value, followed by a line terminator string, to the output stream. (Inherited from HtmlTextWriter.)
Public methodWriteLine(Char)Writes any pending tab spacing and a Unicode character, followed by a line terminator string, to the output stream. (Inherited from HtmlTextWriter.)
Public methodWriteLine(Char[])Writes any pending tab spacing and an array of Unicode characters, followed by a line terminator string, to the output stream. (Inherited from HtmlTextWriter.)
Public methodWriteLine(Decimal)Writes the text representation of a decimal value followed by a line terminator to the text string or stream. (Inherited from TextWriter.)
Public methodWriteLine(Double)Writes any pending tab spacing and the text representation of a double-precision floating-point number, followed by a line terminator string, to the output stream. (Inherited from HtmlTextWriter.)
Public methodWriteLine(Int32)Writes any pending tab spacing and the text representation of a 32-byte signed integer, followed by a line terminator string, to the output stream. (Inherited from HtmlTextWriter.)
Public methodWriteLine(Int64)Writes any pending tab spacing and the text representation of a 64-byte signed integer, followed by a line terminator string, to the output stream. (Inherited from HtmlTextWriter.)
Public methodWriteLine(Object)Writes any pending tab spacing and the text representation of an object, followed by a line terminator string, to the output stream. (Inherited from HtmlTextWriter.)
Public methodWriteLine(Single)Writes any pending tab spacing and the text representation of a single-precision floating-point number, followed by a line terminator string, to the output stream. (Inherited from HtmlTextWriter.)
Public methodWriteLine(String)Writes any pending tab spacing and a text string, followed by a line terminator string, to the output stream. (Inherited from HtmlTextWriter.)
Public methodWriteLine(UInt32)Writes any pending tab spacing and the text representation of a 4-byte unsigned integer, followed by a line terminator string, to the output stream. (Inherited from HtmlTextWriter.)
Public methodWriteLine(UInt64)Writes the text representation of an 8-byte unsigned integer followed by a line terminator to the text string or stream. (Inherited from TextWriter.)
Public methodWriteLine(String, Object)Writes any pending tab spacing and a formatted string containing the text representation of an object, followed by a line terminator string, to the output stream. (Inherited from HtmlTextWriter.)
Public methodWriteLine(String, Object[])Writes any pending tab spacing and a formatted string that contains the text representation of an object array, followed by a line terminator string, to the output stream. (Inherited from HtmlTextWriter.)
Public methodWriteLine(Char[], Int32, Int32)Writes any pending tab spacing and a subarray of Unicode characters, followed by a line terminator string, to the output stream. (Inherited from HtmlTextWriter.)
Public methodWriteLine(String, Object, Object)Writes any pending tab spacing and a formatted string that contains the text representation of two objects, followed by a line terminator string, to the output stream. (Inherited from HtmlTextWriter.)
Public methodWriteLine(String, Object, Object, Object)Writes out a formatted string and a new line, using the same semantics as Format. (Inherited from TextWriter.)
Public methodWriteLineAsync()Writes a line terminator asynchronously to the text string or stream. (Inherited from TextWriter.)
Public methodWriteLineAsync(Char)Writes a character followed by a line terminator asynchronously to the text string or stream. (Inherited from TextWriter.)
Public methodWriteLineAsync(Char[])Writes an array of characters followed by a line terminator asynchronously to the text string or stream. (Inherited from TextWriter.)
Public methodWriteLineAsync(String)Writes a string followed by a line terminator asynchronously to the text string or stream. (Inherited from TextWriter.)
Public methodWriteLineAsync(Char[], Int32, Int32)Writes a subarray of characters followed by a line terminator asynchronously to the text string or stream. (Inherited from TextWriter.)
Public methodWriteLineNoTabsWrites a string, followed by a line terminator string, to the output stream. This method ignores any specified tab spacing. (Inherited from HtmlTextWriter.)
Public methodWriteStyleAttribute(String, String)Writes the specified style attribute to the output stream. (Inherited from HtmlTextWriter.)
Public methodWriteStyleAttribute(String, String, Boolean)Writes the specified style attribute and value to the output stream, and encodes the value, if specified. (Inherited from HtmlTextWriter.)
Protected methodWriteUrlEncodedStringWrites the specified string, encoding it according to URL requirements. (Inherited from HtmlTextWriter.)
Top
  NameDescription
Protected fieldCoreNewLineStores the newline characters used for this TextWriter. (Inherited from TextWriter.)
Top

XHTML is an XML-compliant markup language, based on HTML 4.1, which allows you to create Web sites that are suitable for multiple device types. It merges the ease of use provided by HTML with the strict element guidelines provided by XML to produce a markup language with a wide range of format and style options, and with reduced markup tag ambiguity. The XhtmlTextWriter class provides formatting capabilities that ASP.NET server controls use when rendering XHTML content to clients. You can use the SetDocType method to specify which type of XHTML the text writer renders. Supported document types are defined in the XhtmlMobileDocType enumeration.

The XhtmlTextWriter class renders two sets of attributes for elements. One set is a collection of common attributes, as referenced in the CommonAttributes property. The second set is a collection of element-specific attributes, as referenced in the ElementSpecificAttributes property. For more information on the elements and styles that are rendered, see the XHTML modularization specification at the World Wide Web Consortium (W3C) Web site.

You can use the members of the XhtmlTextWriter class and any derived classes to create custom text writers to use in custom XHTML page adapters or class adapters. You can also create derived classes that override the standard behavior of the XhtmlTextWriter class.

By default, when you are working with browsers that support HTML 4.0, ASP.NET pages and controls render markup that is compatible with the XHTML 1.1 standard. For more information, see XHTML Standards in Visual Studio and ASP.NET.

The HtmlTextWriter outputs XHTML unless you configure ASP.NET specifically to not render XHTML markup. For more information, see How to: Configure ASP.NET Web Sites for Non-XHTML Rendering.

The code example in this section contains four parts. The first example demonstrates how to create a derived class. The second code example demonstrates how to create a custom control. The third code example demonstrates how to use the custom control. The fourth code example provides the code that is required to run the custom control.

The following code example demonstrates how to create a custom class that is derived from the XhtmlTextWriter class. It has two constructors, which is standard for all classes that inherit directly or indirectly from the HtmlTextWriter class. The first constructor takes a TextWriter object as a parameter and calls the second constructor, passing the following two parameter values:

This code example also shows how to override the OnAttributeRender and OnStyleAttributeRender methods to filter for text size and color style, respectively. Additionally, it overrides the BeginRender and EndRender methods to write a text string before and after a control has rendered.

using System;
using System.IO;
using System.Web;
using System.Security.Permissions;
using System.Web.UI;
using System.Web.UI.Adapters;
using System.Web.UI.WebControls.Adapters;

namespace Samples.AspNet.CS
{
    // Create a class that inherits from XhtmlTextWriter.
    [AspNetHostingPermission(SecurityAction.Demand, 
        Level=AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, 
        Level=AspNetHostingPermissionLevel.Minimal)] 
    public class CustomXhtmlTextWriter : XhtmlTextWriter
    {
        // Create two constructors, following  
        // the pattern for implementing a 
        // TextWriter constructor. 
        public CustomXhtmlTextWriter(TextWriter writer) : 
            this(writer, DefaultTabString)
        {
        }


        public CustomXhtmlTextWriter(TextWriter writer, string tabString) : 
            base(writer, tabString)
        {
        }


        // Override the OnAttributeRender method to  
        // allow this text writer to render only eight-point  
        // text size. 
        protected override bool OnAttributeRender(string name, 
          string value, 
          HtmlTextWriterAttribute key) 
        {
            if (key == HtmlTextWriterAttribute.Size)
            {
                if (String.Compare(value, "8pt") == 0)
                {
                    return true;
                }
                else
                {
                   return false;
                } 
             }
             else
             {
                 return base.OnAttributeRender(name, value, key);
             }

         }

        // Override the OnStyleAttributeRender 
        // method to prevent this text writer  
        // from rendering purple text. 
        protected override bool OnStyleAttributeRender(string name, 
            string value, 
            HtmlTextWriterStyle key)
        {
            if (key == HtmlTextWriterStyle.Color)
            {
                if (String.Compare(value, "purple") == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return base.OnStyleAttributeRender(name, value, key);
            }        
        }  

        // Override the BeginRender method to write a 
        // message and call the WriteBreak method 
        // before a control is rendered. 
        override public void BeginRender()
        {
           this.Write("A control is about to render.");
           this.WriteBreak();
        }

        // Override the EndRender method to 
        // write a string immediately after  
        // a control has rendered.  
        override public void EndRender()
        {
           this.Write("A control just rendered.");
        }  

    }
}

The following code example demonstrates how to create a custom Label control named TestLabel and a custom adapter named XhtmlTestLabelAdapter that renders the content of the control as XHTML.

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.Adapters;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.Adapters;

namespace AspNet.Samples
{
	// Create a simple class that inherits 
    // from the Label class. 
	public class TestLabel : Label
	{
		private String _textValue;

		// Override the Text property. 
		public override string Text
		{
			get
			{
				return (string)ViewState["Text"];
			}
			set
			{
				ViewState["Text"] = value;
			}
		}
	}
	public class XhtmlTestLabelAdapter : WebControlAdapter
	{
		// Create a control property that accesses the 
		// methods and properties of the control. 
		protected TestLabel Control
		{
			get
			{
				return (TestLabel)base.Control;
			}
		}

		protected override void Render(HtmlTextWriter writer)
		{
			// Create an instance of the XhtmlTextWriter class, 
			// named w, and cast the HtmlTextWriter passed  
			// in the writer parameter to w.
			XhtmlTextWriter w = new XhtmlTextWriter(writer);

			// Create a string variable, named value, to hold 
			// the control's Text property value.
			String value = Control.Text;

			
            // Create a Boolean variable, named attTest, 
            // to test whether the Style attribute is  
            // valid in the page that the control is 
            // rendered to.
            Boolean attTest = w.IsValidFormAttribute("style");

            // Check whether attTest is true or false. 
            // If true, a style is applied to the XHTML 
            // content. If false, no style is applied. 
			if (attTest)
				w.EnterStyle(Control.ControlStyle);

			// Write the Text property value of the control, 
            // a <br> element, and a string. Consider encoding the value using WriteEncodedText.
            w.Write(value);
            w.WriteBreak();
            w.Write("This control conditionally rendered its styles for XHTML.");

            // Check whether attTest is true or false. 
            // If true, the XHTML style is closed. 
            // If false, nothing is rendered. 
            if (attTest)
                w.ExitStyle(Control.ControlStyle);
		}

	}

}

The following code example demonstrates how to use the custom control TestLabel on an ASP.NET Web page.

<%@ Page Language="C#" %>
<%@ Import Namespace="AspNet.Samples" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  protected void Page_Load(object sender, EventArgs e)
  {
    TestLabel tl = new TestLabel();
    tl.ID = "TestLabel1";
    PlaceHolder1.Controls.Add(tl);

  }
</script>


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>XHtmlTextWriter Example</title>
</head>
<body>
    <form id="form1" runat="server" >
    <div>
      <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>    
    </div>
    </form>
</body>
</html>

To use the custom control in the preceding code example, add the following <controlAdapters> element to one of two files. You can add it to the appropriate machine-wide file in the subdirectory for a specific browser, as a subfolder of the .NET Framework configuration directory. Alternatively, you can add it to a custom browser file in the App_Browsers directory under the Web application root.

<controlAdapters>
   <adapter controlType="AspNet.Samples.TestLabel"
   adapterType="AspNet.Samples.XhtmlTestLabelAdapter" />
</controlAdapters>

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.