HtmlTextWriter.FilterAttributes Method
Filters all HTML attributes and style attributes by calling HtmlTextWriter.OnAttributeRender and HtmlTextWriter.OnStyleAttributeRender on all properites of the page or ASP.NET server control.
[Visual Basic] Protected Overridable Sub FilterAttributes() [C#] protected virtual void FilterAttributes(); [C++] protected: virtual void FilterAttributes(); [JScript] protected function FilterAttributes();
Remarks
Override this method only when inheriting from the HtmlTextWriter class. Before rendering attributes on an HTML element, this method is called. In turn, it calls OnAttributeRender and OnStyleAttributeRender for each attribute and style that is to be rendered.
Example
[Visual Basic, C#, C++] The following example is a custom class that extends the HtmlTextWriter class. It overrides the FilterAttributes method to check whether an HTML <label> element or an HTML <a> (anchor) element is being rendered. If a <label> element is being rendered, it checks whether an HTML 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, it determines whether an href attribute is included and, if not, adds an href to the URL http://www.cohowinery.com.
[Visual Basic] ' Override the FilterAttributes method to filter for ' an HTML label and an anchor elements' attributes. Protected Overrides Sub 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 TagKey = HtmlTextWriterTag.Label Then If Not IsAttributeDefined(HtmlTextWriterAttribute.Style) Then AddAttribute("style", EncodeAttributeValue("color:blue", True)) Write(NewLine) Indent = 3 OutputTabs() End If End If ' 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 TagKey = HtmlTextWriterTag.A Then If Not IsAttributeDefined(HtmlTextWriterAttribute.Href) Then AddAttribute("href", EncodeUrl("http://www.cohowinery.com")) End If End If ' Call the base class's FilterAttributes method ' to ensure that calling this custom HtmlTextWriter ' includes functionality for all other HTML elements. MyBase.FilterAttributes() End Sub 'FilterAttributes [C#] // Override the FilterAttributes method to filter for // an HTML label and an anchor elements' attributes. protected override 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(TagKey == HtmlTextWriterTag.Label) { if(!IsAttributeDefined(HtmlTextWriterAttribute.Style)) { AddAttribute("style", EncodeAttributeValue("color:blue", true)); Write(NewLine); 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(TagKey == HtmlTextWriterTag.A) { if(!IsAttributeDefined(HtmlTextWriterAttribute.Href)) { AddAttribute("href", EncodeUrl("http://www.cohowinery.com")); } } // Call the base class's FilterAttributes method // to ensure that calling this custom HtmlTextWriter // includes functionality for all other HTML elements. base.FilterAttributes(); } [C++] // Override the FilterAttributes method to filter for // an HTML label and an anchor elements' 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 (TagKey == HtmlTextWriterTag::Label) { if (!IsAttributeDefined(HtmlTextWriterAttribute::Style)) { AddAttribute(S"style", EncodeAttributeValue(S"color:blue", true)); Write(NewLine); 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 (TagKey == HtmlTextWriterTag::A) { if (!IsAttributeDefined(HtmlTextWriterAttribute::Href)) { AddAttribute(S"href", EncodeUrl(S"http://www.cohowinery.com")); } } // Call the base class's FilterAttributes method // to ensure that calling this custom HtmlTextWriter // includes functionality for all other HTML elements. __super::FilterAttributes(); }
[Visual Basic, C#, C++]
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
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