HtmlTextWriter.OnAttributeRender 方法

定义

确定是否可向当前标记元素呈现指定的标记属性及其值。

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

参数

name
String

包含要呈现的属性名称的字符串。

value
String

包含要分配给该属性的值的字符串。

key
HtmlTextWriterAttribute

与标记属性关联的 HtmlTextWriterAttribute

返回

Boolean

总是为 true

示例

下面的代码示例演示如何重写 OnAttributeRender 该方法。 If a Size attribute is rendered, but the Size value is not 30pt, the OnAttributeRender override calls the AddAttribute method to add a Size attribute and set its value to 30pt.

// If a size attribute is to be rendered, compare its value to 30 point.
// If it is not set to 30 point, add the attribute and set the value to 30,
// then return false.
protected override bool OnAttributeRender(string name,
    string value,
    HtmlTextWriterAttribute key)
{

    if (key == HtmlTextWriterAttribute.Size)
    {
        if (string.Compare(value, "30pt") != 0)
        {
            AddAttribute("size", "30pt");
            return false;
        }
    }

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

    If key = HtmlTextWriterAttribute.Size Then
        If [String].Compare(value, "30pt") <> 0 Then
            AddAttribute("size", "30pt")
            Return False
        End If
    End If

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

注解

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

继承者说明

如果继承自 HtmlTextWriter 类,则可以重写 OnAttributeRender(String, String, HtmlTextWriterAttribute) 该方法以返回 false ,以防止属性完全呈现、在特定元素上呈现或为特定标记呈现。 例如,如果不希望从HtmlTextWriter派生的对象将属性呈现bgcolor<table>元素,则可以在传递TagNamebgcolorname重写OnAttributeRender(String, String, HtmlTextWriterAttribute)并返回false属性值table

适用于

另请参阅