HtmlTextWriter.OnAttributeRender(String, String, HtmlTextWriterAttribute) HtmlTextWriter.OnAttributeRender(String, String, HtmlTextWriterAttribute) HtmlTextWriter.OnAttributeRender(String, String, HtmlTextWriterAttribute) HtmlTextWriter.OnAttributeRender(String, String, HtmlTextWriterAttribute) Method

定義

判斷指定的標記屬性以及該屬性的值是否可以在目前的標記項目中呈現。Determines whether the specified markup attribute and its value can be rendered to the current markup element.

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 String String String

字串,包含要呈現的屬性名稱。A string containing the name of the attribute to render.

value
String String String String

字串,包含要指派給屬性的值。A string containing the value that is assigned to the attribute.

傳回

一定是 trueAlways true.

範例

下列程式碼範例顯示如何覆寫OnAttributeRender方法。The following code example shows how to override the OnAttributeRender method. OnAttributeRender 30pt AddAttribute 30pt如果呈現Size屬性, Size但值不是,則覆寫會呼叫方法來加入屬性,並將其值設定為。 SizeIf a Size attribute is rendered, but the Size value is not 30pt, the OnAttributeRender override calls the AddAttribute method to add a Size attribute and set its value to 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 true作為一律會傳回。 OnAttributeRenderThe HtmlTextWriter class implementation of the OnAttributeRender method always returns true. OnAttributeRender覆寫可以判斷屬性是否會轉譯至頁面。The OnAttributeRender overrides can determine whether an attribute will be rendered to the page.

給繼承者的注意事項

如果您繼承自HtmlTextWriter類別,您可以覆OnAttributeRender(String, String, HtmlTextWriterAttribute)寫方法以false傳回,以防止屬性完全轉譯、在特定專案上轉譯,或是針對特定標記呈現。If you inherit from the HtmlTextWriter class, you can override the OnAttributeRender(String, String, HtmlTextWriterAttribute) method to return false to prevent an attribute from being rendered at all, being rendered on a particular element, or being rendered for a particular markup. 例如,如果您不希望HtmlTextWriter衍生自的物件將bgcolor屬性轉譯為<table>專案,您可以覆寫OnAttributeRender(String, String, HtmlTextWriterAttribute)false在通過和時傳回bgcolor name屬性值為tableTagNameFor example, if you do not want the object derived from HtmlTextWriter to render the bgcolor attribute to <table> elements, you can override the OnAttributeRender(String, String, HtmlTextWriterAttribute) and return false when name passes bgcolor and the TagName property value is table.

適用於

另請參閱