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 ()
protected void FilterAttributes ()
protected function FilterAttributes ()
Not applicable.

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 http://www.cohowinery.com.

// Override the FilterAttributes method to check whether 
// <label> and <anchor> elements contain specific attributes. 
virtual void FilterAttributes() override
{
   // 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 ) );
         Write( NewLine );
         Indent = 3;
         OutputTabs();
      }
   }

   // 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 http://www.cohowinery.com.
   // Use the EncodeUrl method to convert any spaces to %20.
   if ( TagKey == HtmlTextWriterTag::A )
   {
      if (  !IsAttributeDefined( HtmlTextWriterAttribute::Href ) )
      {
         AddAttribute( "href", EncodeUrl( "http://www.cohowinery.com" ) );
      }
   }

   // Call the FilterAttributes method of the base class.
   __super::FilterAttributes();
}

// Override the FilterAttributes method to check whether 
// <label> and <anchor> elements contain specific attributes.      
protected void FilterAttributes()
{
    // If the Label tag is being rendered and a style
    // attribute is not defined, add a style attribute 
    // and set its value to blue.
    if (get_TagKey().Equals(HtmlTextWriterTag.Label)) {
        if (!(IsAttributeDefined(HtmlTextWriterAttribute.Style))) {
            AddAttribute("style", EncodeAttributeValue("color:blue",
                true));
            Write(get_NewLine());
            set_Indent(3);
            OutputTabs();
        }
    }

    // If an Anchor element is being rendered and an href
    // attribute has not been defined, call the AddAttribute
    // method to add an href
    // attribute and set it to http://www.cohowinery.com.
    // Use the EncodeUrl method to convert any spaces to %20.
    if (get_TagKey().Equals(HtmlTextWriterTag.A)) {
        if (!(IsAttributeDefined(HtmlTextWriterAttribute.Href))) {
            AddAttribute("href", EncodeUrl("http://www.cohowinery.com"));
        }
    }

    // Call the FilterAttributes method of the base class.
    super.FilterAttributes();
} //FilterAttributes

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show: