HtmlTextWriter.GetAttributeKey(String) 方法

定义

获取指定属性的相应 HtmlTextWriterAttribute 枚举值。

C#
protected System.Web.UI.HtmlTextWriterAttribute GetAttributeKey(string attrName);

参数

attrName
String

包含要为其获取 HtmlTextWriterAttribute 的属性的字符串。

返回

指定属性的 HtmlTextWriterAttribute 枚举值;否则,如果该属性不是该枚举的成员,则为无效的 HtmlTextWriterAttribute 值。

示例

下面的代码示例演示如何使用派生自 HtmlTextWriter 类的类来替代 RenderBeginTag 方法。 重写检查 是否 tagKey 等于 Font 字段,这指示 <font> 将呈现标记元素。 如果是这样,重写将调用 IsAttributeDefined 方法,以确定元素是否 <font> 包含 特性 Size 。 如果 返回 false,该方法AddAttribute将调用 GetAttributeKey 方法,该方法定义 Size 并将其值设置为 30ptIsAttributeDefined

C#
// If the tagKey parameter is set to a <font> element
// but a size attribute is not defined on the element,
// the AddStyleAttribute method adds a size attribute
// and sets it to 30 point. 
if (tagKey == HtmlTextWriterTag.Font)
{
    if (!IsAttributeDefined(HtmlTextWriterAttribute.Size))
    {
        AddAttribute(GetAttributeKey("size"), "30pt");
    }
}

注解

如果 attrNamenull 或空字符串 (“”) ,或者无法在属性名称表中找到,则返回值 -1(为 HtmlTextWriterAttribute 对象键入)。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

另请参阅