HtmlTextWriter.OnAttributeRender(String, String, HtmlTextWriterAttribute) 方法

定义

确定是否可向当前标记元素呈现指定的标记属性及其值。Determines whether the specified markup attribute and its value can be rendered to the current markup element.

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

包含要呈现的属性名称的字符串。A string containing the name of the attribute to render.

value
String

包含要分配给该属性的值的字符串。A string containing the value that is assigned to the attribute.

key
HtmlTextWriterAttribute

与标记属性关联的 HtmlTextWriterAttributeThe HtmlTextWriterAttribute associated with the markup attribute.

返回

总是为 trueAlways true.

示例

下面的代码示例演示如何重写OnAttributeRender方法。The following code example shows how to override the OnAttributeRender method. 30pt AddAttribute OnAttributeRender 如果呈现Size特性但该值不为,则重写将调用方法以添加特性,并将其值设置为30ptSize SizeIf 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

注解

OnAttributeRender方法的true类实现始终返回。 HtmlTextWriterThe HtmlTextWriter class implementation of the OnAttributeRender method always returns true. OnAttributeRender重写可以确定属性是否将呈现到页面。The OnAttributeRender overrides can determine whether an attribute will be rendered to the page.

继承者说明

如果从HtmlTextWriter类继承,则可以OnAttributeRender(String, String, HtmlTextWriterAttribute)重写方法以返回false ,以防止在特定元素上呈现特性,或针对特定标记呈现特性。If you inherit from the HtmlTextWriter class, you can override the OnAttributeRender(String, String, HtmlTextWriterAttribute) method to return false to prevent an attribute from being rendered at all, being rendered on a particular element, or being rendered for a particular markup. 例如,如果您不希望HtmlTextWriter派生自的对象将bgcolor特性呈现给<table>元素,则可以重写OnAttributeRender(String, String, HtmlTextWriterAttribute) ,并在传递bgcolorfalse name返回属性值为tableTagNameFor example, if you do not want the object derived from HtmlTextWriter to render the bgcolor attribute to <table> elements, you can override the OnAttributeRender(String, String, HtmlTextWriterAttribute) and return false when name passes bgcolor and the TagName property value is table.

适用于

另请参阅