HtmlTextWriter.FilterAttributes 方法

定義

移除網頁或 Web 伺服器控制項的所有屬性上的所有標記和樣式屬性。

C#
protected virtual void FilterAttributes ();

範例

下列程式碼範例示範如何使用衍生自 類別的 HtmlTextWriter 自訂類別來覆寫 FilterAttributes 方法。 呼叫時,覆寫會 FilterAttributes 檢查文字寫入器是否轉譯任何 <label><a> 專案:

  • <label>如果正在轉譯專案,此方法會 FilterAttributes 檢查屬性是否 style 在專案上呈現,如果不是,則會建立 style 屬性,並將它設定為 color: blue

  • <a>如果正在轉譯專案, FilterAttributes 方法會判斷是否包含屬性,如果不包含 href ,則會將 加入 href 至 URL http://www.cohowinery.com

C#
// 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();
}

備註

在標記專案上轉譯屬性之前,會 FilterAttributes 呼叫 方法。 接著,方法 FilterAttributes 會針對每個屬性和樣式呼叫 OnAttributeRenderOnStyleAttributeRender 方法,以轉譯。

適用於

產品 版本
.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

另請參閱