HtmlTextWriter.OnStyleAttributeRender(String, String, HtmlTextWriterStyle) HtmlTextWriter.OnStyleAttributeRender(String, String, HtmlTextWriterStyle) HtmlTextWriter.OnStyleAttributeRender(String, String, HtmlTextWriterStyle) HtmlTextWriter.OnStyleAttributeRender(String, String, HtmlTextWriterStyle) Method

定義

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

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

參數

name
String String String String

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

value
String String String String

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

key
HtmlTextWriterStyle HtmlTextWriterStyle HtmlTextWriterStyle HtmlTextWriterStyle

與此樣式屬性相關聯的 HtmlTextWriterStyleThe HtmlTextWriterStyle associated with the style attribute.

傳回

一定是 trueAlways true.

範例

下列程式碼範例顯示如何覆寫OnStyleAttributeRender方法。The following code example shows how to override the OnStyleAttributeRender method. AddStyleAttribute OnStyleAttributeRender Color purple Color如果樣式屬性已轉譯,但值不是,則覆寫會使用方法,將屬性設定為purpleColorIf a Color style attribute is rendered, but the Color value is not purple, the OnStyleAttributeRender override uses the AddStyleAttribute method to set the Color attribute to purple.

// If a color style attribute is to be rendered,
// compare its value to purple. If it is not set to
// purple, add the style attribute and set the value
// to purple, then return false.
protected override bool OnStyleAttributeRender(string name,
    string value,
    HtmlTextWriterStyle key)
{

    if (key == HtmlTextWriterStyle.Color)
    {
        if (string.Compare(value, "purple") != 0)
        {
            AddStyleAttribute("color", "purple");
            return false;
        }
    }

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

    If key = HtmlTextWriterStyle.Color Then
        If [String].Compare(value, "purple") <> 0 Then
            AddStyleAttribute("color", "purple")
            Return False
        End If
    End If

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

備註

方法的類別實HtmlTextWriter true作為一律會傳回。 OnStyleAttributeRenderThe HtmlTextWriter class implementation of the OnStyleAttributeRender method always returns true. OnStyleAttributeRender覆寫可以決定是否要將樣式屬性轉譯至頁面。The OnStyleAttributeRender overrides can determine whether a style attribute will be rendered to the page.

給繼承者的注意事項

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

適用於

另請參閱