语言

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方法将属性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

注解

HtmlTextWriter方法的OnStyleAttributeRender类实现始终返回 true。 重写 OnStyleAttributeRender 可以确定样式属性是否将呈现到页面。

继承者说明

如果继承自 HtmlTextWriter 类,则可以重写 OnStyleAttributeRender(String, String, HtmlTextWriterStyle) 方法以阻止 false 样式属性完全呈现、在特定元素上呈现或为特定标记语言呈现。 例如,如果不希望派生HtmlTextWriter自的对象将样式属性呈现color给<p>元素,则可以在传递OnStyleAttributeRender(String, String, HtmlTextWriterStyle)false时name重写color并返回TagName属性值p。

适用于

另请参阅