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

返回

Boolean

总是为 true

示例

下面的代码示例演示如何重写 OnStyleAttributeRender 该方法。 Color如果呈现样式属性,但Color值不是purple,重写OnStyleAttributeRender将使用AddStyleAttribute方法将属性purple设置为 Color

// 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>元素,则可以在传递TagNamecolorname重写OnStyleAttributeRender(String, String, HtmlTextWriterStyle)并返回false属性值p

适用于

另请参阅