HtmlTextWriter.OnTagRender(String, HtmlTextWriterTag) 方法

定义

确定是否向请求页面呈现指定的标记元素。Determines whether the specified markup element will be rendered to the requesting page.

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

参数

name
String

包含要呈现的元素名称的字符串。A string containing the name of the element to render.

key
HtmlTextWriterTag

与元素关联的 HtmlTextWriterTagThe HtmlTextWriterTag associated with the element.

返回

总是为 trueAlways true.

示例

下面的代码示例演示如何重写OnTagRender方法。The following code example shows how to override the OnTagRender method. 如果正在Font呈现某个元素,则重OnTagRender写将使用IsAttributeDefined方法来确定是否Size正在呈现特性。If a Font element is being rendered, the OnTagRender override uses the IsAttributeDefined method to determine whether a Size attribute is being rendered. 如果不是Size ,则使用AddAttribute方法创建属性,并将其值设置为20ptIf not, it uses the AddAttribute method to create a Size attribute and set its value to 20pt.

// If a <font> element is to be rendered, check whether it contains
// a size attribute. If it does not, add one and set its value to
// 20 points, then return true.
protected override bool OnTagRender(string name, HtmlTextWriterTag key)
{

    if (key == HtmlTextWriterTag.Font)
    {
        if (!(IsAttributeDefined(HtmlTextWriterAttribute.Size)))
        {
            AddAttribute(HtmlTextWriterAttribute.Size, "20pt");
            return true;
        }
    }

    // If the element is not a <font> element, use
    // the base functionality of the OnTagRenderMethod.
    return base.OnTagRender(name, key);
}
' If a <font> element is to be rendered, check whether it contains
' a size attribute. If it does not, add one and set its value to
' 20 points, then return true.
Protected Overrides Function OnTagRender( _
    name As String, _
    key As HtmlTextWriterTag) _
As Boolean

    If (key = HtmlTextWriterTag.Font) Then
        If Not (IsAttributeDefined(HtmlTextWriterAttribute.Size)) Then
            AddAttribute(HtmlTextWriterAttribute.Size, "20pt")
            Return True
        End If
    End If

    ' If the element is not a <font> element, use
    ' the base functionality of the OnTagRenderMethod.
    Return MyBase.OnTagRender(name, key)
End Function

注解

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

继承者说明

如果从HtmlTextWriter类继承,则可以OnTagRender(String, HtmlTextWriterTag)重写方法以返回false ,以防止标记元素呈现为特定的标记语言。If you inherit from the HtmlTextWriter class, you can override the OnTagRender(String, HtmlTextWriterTag) method to return false to prevent a markup element from being rendered at all or for a particular markup language. 例如,如果HtmlTextWriter不希望派生自的对象<font>呈现元素OnTagRender(String, HtmlTextWriterTag) ,则可以重写方法,以便在请求包含<font>元素的页时false返回。For example, if you do not want the object that is derived from HtmlTextWriter to render the <font> element, you can override the OnTagRender(String, HtmlTextWriterTag) method to return false when a page is requested that contains a <font> element.

适用于

另请参阅