共用方式為


HtmlTextWriter.GetStyleKey(String) 方法

定義

取得指定樣式的 HtmlTextWriterStyle 列舉值。

protected:
 System::Web::UI::HtmlTextWriterStyle GetStyleKey(System::String ^ styleName);
protected System.Web.UI.HtmlTextWriterStyle GetStyleKey (string styleName);
member this.GetStyleKey : string -> System.Web.UI.HtmlTextWriterStyle
Protected Function GetStyleKey (styleName As String) As HtmlTextWriterStyle

參數

styleName
String

要取得的 HtmlTextWriterStyle 之樣式屬性。

傳回

HtmlTextWriterStyle

對應到 styleNameHtmlTextWriterStyle 列舉值。

範例

下列程式碼範例示範如何在衍生自 類別的 HtmlTextWriter 類別中覆寫 RenderBeginTag 方法。 覆 RenderBeginTag 寫會判斷標記是否 <label> 要轉譯,如果是,則會檢查項目是否有 Color 屬性。 Color如果尚未定義屬性, GetStyleKey 方法會當做方法呼叫 AddStyleAttribute 中的第一個參數使用,將屬性 <label> 加入 Color 標記專案,並將 屬性設定 Colorred

// Override the RenderBeginTag method to check whether
// the tagKey parameter is set to a <label> element
// or a <font> element.   
virtual void RenderBeginTag( HtmlTextWriterTag tagKey ) override
{
   // If the tagKey parameter is set to a <label> element
   // but a color attribute is not defined on the element,
   // the AddStyleAttribute method adds a color attribute
   // and sets it to red.
   if ( tagKey == HtmlTextWriterTag::Label )
   {
      if (  !IsStyleAttributeDefined( HtmlTextWriterStyle::Color ) )
      {
         AddStyleAttribute( GetStyleKey( "color" ), "red" );
      }
   }

   // 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" );
      }
   }

   // Call the base class's RenderBeginTag method
   // to ensure that calling this custom markup writer
   // includes functionality for all other elements.
   __super::RenderBeginTag( tagKey );
}
// Override the RenderBeginTag method to check whether
// the tagKey parameter is set to a <label> element
// or a <font> element.
public override void RenderBeginTag(HtmlTextWriterTag tagKey)
{

    // If the tagKey parameter is set to a <label> element
    // but a color attribute is not defined on the element,
    // the AddStyleAttribute method adds a color attribute
    // and sets it to red.
    if (tagKey == HtmlTextWriterTag.Label)
    {
        if (!IsStyleAttributeDefined(HtmlTextWriterStyle.Color))
        {
            AddStyleAttribute(GetStyleKey("color"), "red");
        }
    }
    // 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");
        }
    }
    // Call the base class's RenderBeginTag method
    // to ensure that this custom MarkupTextWriter
    // includes functionality for all other markup elements.
    base.RenderBeginTag(tagKey);
}
' Override the RenderBeginTag method to check whether
' the tagKey parameter is set to a <label> element
' or a <font> element.   
Public Overloads Overrides Sub RenderBeginTag(ByVal tagKey As HtmlTextWriterTag)
    ' If the tagKey parameter is set to a <label> element
    ' but a color attribute is not defined on the element,
    ' the AddStyleAttribute method adds a color attribute
    ' and sets it to red.
    If tagKey = HtmlTextWriterTag.Label Then
        If Not IsStyleAttributeDefined(HtmlTextWriterStyle.Color) Then
            AddStyleAttribute(GetStyleKey("color"), "red")
        End If
    End If

    ' 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

    ' Call the base class's RenderBeginTag method
    ' to ensure that this custom MarkupTextWriter
    ' includes functionality for all other markup elements.
    MyBase.RenderBeginTag(tagKey)
End Sub

備註

如果 styleName 值未對應至任何 HtmlTextWriterStyle 列舉值,則 GetStyleKey 方法會傳回類型為 HtmlTextWriterStyle 值 -1 的值。

適用於

另請參閱