HtmlTextWriter.OnAttributeRender 方法

定義

判斷指定的標記屬性以及該屬性的值是否可以在目前的標記項目中呈現。

protected:
 virtual bool OnAttributeRender(System::String ^ name, System::String ^ value, System::Web::UI::HtmlTextWriterAttribute key);
protected virtual bool OnAttributeRender (string name, string value, System.Web.UI.HtmlTextWriterAttribute key);
abstract member OnAttributeRender : string * string * System.Web.UI.HtmlTextWriterAttribute -> bool
override this.OnAttributeRender : string * string * System.Web.UI.HtmlTextWriterAttribute -> bool
Protected Overridable Function OnAttributeRender (name As String, value As String, key As HtmlTextWriterAttribute) As Boolean

參數

name
String

字串,包含要呈現的屬性名稱。

value
String

字串,包含要指派給屬性的值。

key
HtmlTextWriterAttribute

與標記屬性關聯的 HtmlTextWriterAttribute

傳回

一定是 true

範例

下列程式碼範例示範如何覆寫 OnAttributeRender 方法。 Size如果呈現屬性,但 Size 值不是 30pt ,覆寫會 OnAttributeRender 呼叫 AddAttribute 方法以新增 Size 屬性,並將其值設定為 30pt

// If a size attribute is to be rendered, compare its value to 30 point.
// If it is not set to 30 point, add the attribute and set the value to 30,
// then return false.
protected override bool OnAttributeRender(string name,
    string value,
    HtmlTextWriterAttribute key)
{

    if (key == HtmlTextWriterAttribute.Size)
    {
        if (string.Compare(value, "30pt") != 0)
        {
            AddAttribute("size", "30pt");
            return false;
        }
    }

    // If the attribute is not a size attribute, use
    // the base functionality of the OnAttributeRender method.
    return base.OnAttributeRender(name, value, key);
}
' If a size attribute is to be rendered, compare its value to 30 point.
' If it is not set to 30 point, add the attribute and set the value to 30
' then return false.
Protected Overrides Function OnAttributeRender(name As String, _
    value As String, _
    key As HtmlTextWriterAttribute) _
As Boolean

    If key = HtmlTextWriterAttribute.Size Then
        If [String].Compare(value, "30pt") <> 0 Then
            AddAttribute("size", "30pt")
            Return False
        End If
    End If

    ' If the attribute is not a size attribute, use
    ' the base functionality of the OnAttributeRender method.
    Return MyBase.OnAttributeRender(name, value, key)
End Function 'OnAttributeRender

備註

方法 HtmlTextWriter 的類別實作 OnAttributeRender 一律會傳 true 回 。 覆 OnAttributeRender 寫可以判斷屬性是否會轉譯至頁面。

給繼承者的注意事項

如果您繼承自 HtmlTextWriter 類別,您可以覆寫 OnAttributeRender(String, String, HtmlTextWriterAttribute) 方法以傳回 false ,以防止屬性完全轉譯、在特定專案上轉譯,或針對特定標記轉譯。 例如,如果您不想讓衍生自 HtmlTextWriter 的物件將屬性轉 bgcolor 譯為 <table> 專案,您可以在傳遞 bgcolorname 覆寫 OnAttributeRender(String, String, HtmlTextWriterAttribute)TagName 傳回 false ,而屬性值為 table

適用於

另請參閱