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 屬性。 IsAttributeDefined如果 傳回 false ,則 AddAttribute 方法會呼叫 GetAttributeKey 方法,這個方法會 Size 定義 並將其值設定為 30pt

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

另請參閱