HtmlTextWriter.FilterAttributes Method

Definition

Supprime tous les attributs de style et de balisage sur toutes les propriétés de la page ou du contrôle serveur Web.Removes all the markup and style attributes on all properties of the page or Web server control.

protected:
 virtual void FilterAttributes();
protected virtual void FilterAttributes ();
abstract member FilterAttributes : unit -> unit
override this.FilterAttributes : unit -> unit
Protected Overridable Sub FilterAttributes ()

Examples

L’exemple de code suivant montre comment utiliser une classe personnalisée, dérivée de la classe HtmlTextWriter, qui substitue la méthode FilterAttributes.The following code example shows how to use a custom class, derived from the HtmlTextWriter class, that overrides the FilterAttributes method. Lorsqu’elle est appelée, la FilterAttributes Substitue vérifie si le writer de texte restitue des éléments <label> ou <a> :When called, the FilterAttributes override checks whether the text writer renders any <label> or <a> elements:

  • Si un élément <label> est rendu, la méthode FilterAttributes vérifie si un attribut style est rendu sur l’élément et, si ce n’est pas le cas, crée un style attribut et lui affecte la valeur color: blue.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.

  • Si un élément <a> est rendu, la méthode FilterAttributes détermine si un attribut href est inclus et, si ce n’est pas le cas, ajoute un href à l’URL http://www.cohowinery.com.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 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));
            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.
    base.FilterAttributes();
}
' Override the FilterAttributes method to check whether 
' <label> and <anchor> elements contain specific attributes.   
Protected Overrides Sub 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 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 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 FilterAttributes method of the base class.
    MyBase.FilterAttributes()
End Sub

Remarks

Avant que les attributs soient rendus sur un élément de balisage, la méthode FilterAttributes est appelée.Before attributes are rendered on a markup element, the FilterAttributes method is called. À son tour, la méthode FilterAttributes appelle les méthodes OnAttributeRender et OnStyleAttributeRender pour chaque attribut et style à restituer.In turn, the FilterAttributes method calls the OnAttributeRender and OnStyleAttributeRender methods for each attribute and style to render.

Applies to

See also