HtmlTextWriter.GetAttributeKey(String) 方法

定義

取得指定屬性的對應 HtmlTextWriterAttribute 列舉值。

protected:
 System::Web::UI::HtmlTextWriterAttribute GetAttributeKey(System::String ^ attrName);
protected System.Web.UI.HtmlTextWriterAttribute GetAttributeKey (string attrName);
member this.GetAttributeKey : string -> System.Web.UI.HtmlTextWriterAttribute
Protected Function GetAttributeKey (attrName As String) As HtmlTextWriterAttribute

參數

attrName
String

字串,包含要取得的 HtmlTextWriterAttribute 之屬性。

傳回

指定屬性的 HtmlTextWriterAttribute 列舉值,如果該屬性不是列舉型別的成員,則為無效的 HtmlTextWriterAttribute 值。

範例

下列程式碼範例示範如何使用衍生自 類別的 HtmlTextWriter 類別來覆寫 RenderBeginTag 方法。 覆寫會檢查是否 tagKey 等於 Font 欄位,這表示 <font> 將轉譯標記專案。 如果是,覆寫會呼叫 IsAttributeDefined 方法,以找出專案是否 <font> 包含 Size 屬性。 IsAttributeDefined如果 傳回 false ,則 AddAttribute 方法會呼叫 GetAttributeKey 方法,這個方法會 Size 定義 並將其值設定為 30pt

// 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" );
   }
}
// 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");
    }
}
' 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 Then
    If Not IsAttributeDefined(HtmlTextWriterAttribute.Size) Then
        AddAttribute(GetAttributeKey("size"), "30pt")
    End If
End If

備註

如果 attrNamenull 或空字串 (「」「) ,或找不到屬性名稱的資料表,則會傳回值 -1,輸入物件 HtmlTextWriterAttribute

適用於

另請參閱