HtmlTextWriter.OnStyleAttributeRender 方法

定義

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

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

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

value
String

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

key
HtmlTextWriterStyle

與此樣式屬性相關聯的 HtmlTextWriterStyle

傳回

一定是 true

範例

下列程式碼範例示範如何覆寫 OnStyleAttributeRender 方法。 如果呈現 Color 樣式屬性,但 Color 值不是 purple ,則 OnStyleAttributeRender 覆寫會使用 AddStyleAttribute 方法將 屬性設定 Colorpurple

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

備註

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

給繼承者的注意事項

如果您繼承自 HtmlTextWriter 類別,您可以覆寫 OnStyleAttributeRender(String, String, HtmlTextWriterStyle) 方法以傳回 false 以防止樣式屬性完全轉譯、在特定專案上轉譯,或針對特定標記語言轉譯。 例如,如果您不想讓衍生自 HtmlTextWriter 的物件將樣式屬性轉 color 譯為 <p> 元素,您可以在傳遞 colorname 覆寫 OnStyleAttributeRender(String, String, HtmlTextWriterStyle) 並傳回 false ,而 TagName 屬性值為 p

適用於

另請參閱