HtmlTextWriter.TagName Property

Definition

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

C#
protected string TagName { get; set; }

Property Value

The tag name of the markup element being rendered.

Examples

The following code example demonstrates an overridden version of the RenderBeforeTag method in a class that derives from the HtmlTextWriter class. The code example checks whether the element to render is a <label> element by calling the String.Compare method, and then passing the TagName property value and a string, "label", as the parameter arguments. 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> element's opening tag. If the element to render is not a <label> element, the base class's version of the RenderBeforeTag method is called.

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 markup writer. 
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();
    }
}

Remarks

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

Applies to

Product Versions
.NET Framework 1.1, 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

See also