語言

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

一個包含要渲染 style 屬性名稱的字串。

value
String

一個包含被指派給 style 屬性的值的字串。

key
HtmlTextWriterStyle

與 HtmlTextWriterStyle 風格屬性相關。

傳回

一律為 true。

範例

以下程式碼範例說明如何覆寫該 OnStyleAttributeRender 方法。 如果Color渲染了一個樣式屬性,但Color值未purpleOnStyleAttributeRender被渲染,覆寫會使用該AddStyleAttribute方法將該屬性設Color為 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

備註

該方法的HtmlTextWriterOnStyleAttributeRender類別實作總是回傳 true。 OnStyleAttributeRender覆寫可以決定樣式屬性是否會被渲染到頁面上。

給繼承者的注意事項

如果你是從 HtmlTextWriter 該類別繼承,可以覆寫 OnStyleAttributeRender(String, String, HtmlTextWriterStyle) 回退 false 的方法,以防止某個樣式屬性被渲染、無法渲染到特定元素,或是被渲染到特定標記語言。 例如,如果你不希望HtmlTextWriter衍生物件渲染 color style 屬性到某個<p>元素,你可以覆寫 並在OnStyleAttributeRender(String, String, HtmlTextWriterStyle)通過false時回傳namecolor,且TagName屬性值為 p。

適用於

另請參閱