This documentation is archived and is not being maintained.

HtmlTextWriter.TagName Property

Gets or sets the tag name of the HTML element being rendered.

[Visual Basic]
Protected Property TagName As String
[C#]
protected string TagName {get; set;}
[C++]
protected: __property String* get_TagName();
protected: __property void set_TagName(String*);
[JScript]
protected function get TagName() : String;
protected function set TagName(String);

Property Value

The tag name of the HTML element being rendered.

Remarks

This property is of use only to classes that inherit HtmlTextWriter. You should read or set this property only in RenderBeginTag method calls; this is the only time it is set to a consistent value.

Example

[Visual Basic, C#, C++] The following example shows a custom HtmlTextWriter class that overrides the RenderBeforeTag and RenderAfterTag methods. In each overridden method, the String.Compare method compares the current value of the TagName property with the string "label" to determine whether the HTML element being rendering is a < label > element. In the RenderBeforeTag method, if a < label > element is about to be rendered, the opening tag of a < font > element, with a color attribute set to red, is rendered before the label. If a < label > element has been rendered, the RenderAfterTag method renders the closing tag of a < font > element after the label.

[Visual Basic] 
' Override the RenderBeforeTag method to add the 
' opening tag of a Font element before the 
' opening tag of any Label elements rendered by this 
' custom HtmlTextWriter. 
Protected Overrides Function RenderBeforeTag() As String
   ' Compare the TagName property value to the
   ' string label to determine whether the element to 
   ' be rendered is a Label. If it is a Label,
   ' the opening tag of the Font element, with a Color
   ' style attribute set to red, is added before
   ' the Label.
   If String.Compare(TagName, "label") = 0 Then
      Return "<font color=""red"">"
   ' If a Label is not being rendered, use 
   ' the base RenderBeforeTag method.
   Else
      Return MyBase.RenderBeforeTag()
   End If
End Function 'RenderBeforeTag


[C#] 
// Override the RenderBeforeTag method to add the 
// opening tag of a Font element before the 
// opening tag of any Label elements rendered by this 
// custom HtmlTextWriter. 
protected override string RenderBeforeTag()
{
   // Compare the TagName property value to the
   // string label to determine whether the element to 
   // be rendered is a Label. If it is a Label,
   // the opening tag of the Font element, with a Color
   // style attribute set to red, is added before
   // the Label.
   if(String.Compare(TagName, "label") == 0)
   {
      return "<font color=\"red\">";
   }
   // If a Label is not being rendered, use 
   // the base RenderBeforeTag method.
   else
   {
      return base.RenderBeforeTag();
   }
}

[C++] 
// Override the RenderBeforeTag method to add the
// opening tag of a Font element before the
// opening tag of any Label elements rendered by this
// custom HtmlTextWriter.
protected:
String* RenderBeforeTag() {
   // Compare the TagName property value to the
   // String* label to determine whether the element to
   // be rendered is a Label. If it is a Label,
   // the opening tag of the Font element, with a Color
   // style attribute set to red, is added before
   // the Label.
   if (String::Compare(TagName, S"label") == 0) {
      return S"<font color=\"red\">";
   }
   // If a Label is not being rendered, use
   // the base RenderBeforeTag method.
   else {
      return __super::RenderBeforeTag();
   }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

HtmlTextWriter Class | HtmlTextWriter Members | System.Web.UI Namespace | TagKey

Show: