HtmlTextWriter.FilterAttributes Method ()


Removes all the markup and style attributes on all properties of the page or Web server control.

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

protected virtual void FilterAttributes()

Before attributes are rendered on a markup element, the FilterAttributes method is called. In turn, the FilterAttributes method calls the OnAttributeRender and OnStyleAttributeRender methods for each attribute and style to render.

The following code example shows how to use a custom class, derived from the HtmlTextWriter class, that overrides the FilterAttributes method. When called, the FilterAttributes override checks whether the text writer renders any <label> or <a> elements:

  • If a <label> element is being rendered, the FilterAttributes method checks whether a style attribute is rendered on the element and, if not, creates a style attribute and sets it to color: blue.

  • If an <a> element is being rendered, the FilterAttributes method determines whether an href attribute is included and, if not, adds an href to the URL

// Override the FilterAttributes method to check whether 
// <label> and <anchor> elements contain specific attributes.      
protected override void FilterAttributes()
    // If the <label> element is rendered and a style
    // attribute is not defined, add a style attribute 
    // and set its value to blue.
    if (TagKey == HtmlTextWriterTag.Label)
        if (!IsAttributeDefined(HtmlTextWriterAttribute.Style))
            AddAttribute("style", EncodeAttributeValue("color:blue", true));
            Indent = 3;

    // If an <anchor> element is rendered and an href
    // attribute has not been defined, call the AddAttribute
    // method to add an href attribute
    // and set it to
    // Use the EncodeUrl method to convert any spaces to %20.
    if (TagKey == HtmlTextWriterTag.A)
        if (!IsAttributeDefined(HtmlTextWriterAttribute.Href))
            AddAttribute("href", EncodeUrl(""));
    // Call the FilterAttributes method of the base class.

.NET Framework
Available since 1.1
Return to top