共用方式為


HtmlTextWriter.GetStyleName(HtmlTextWriterStyle) 方法

定義

取得與指定的 HtmlTextWriterStyle 列舉值相關聯的標記樣式屬性名稱。

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

參數

styleKey
HtmlTextWriterStyle

要取得的樣式屬性名稱之 HtmlTextWriterStyle

傳回

styleKey 中所指定的 HtmlTextWriterStyle 列舉值相關聯的樣式屬性名稱。

範例

下列程式碼範例示範如何檢查是否正在轉譯專案 <label> 。 如果是,它會檢查是否已 Color 在 專案上定義樣式屬性。 如果尚未定義屬性,則會呼叫 方法, AddStyleAttribute 而 方法會將 Color 列舉成員轉換成其字串表示,以傳遞至 AddStyleAttributeGetStyleName 方法。

// If the markup element being rendered is a Label,
// render the opening tag of a <Font> element before it.
if ( tagKey == HtmlTextWriterTag::Label )
{
   
   // Check whether a Color style attribute is
   // included on the Label. If not, use the
   // AddStyleAttribute and GetStyleName methods to add one
   // and set its value to red.
   if (  !IsStyleAttributeDefined( HtmlTextWriterStyle::Color ) )
   {
      AddStyleAttribute( GetStyleName( HtmlTextWriterStyle::Color ), "red" );
   }
// If the markup element being rendered is a Label,
// render the opening tag of a Font element before it.
if (tagKey == HtmlTextWriterTag.Label)
{
    // Check whether a Color style attribute is 
    // included on the Label. If not, use the
    // AddStyleAttribute and GetStyleName methods to add one
    // and set its value to red.
    if (!IsStyleAttributeDefined(HtmlTextWriterStyle.Color))
    {
        AddStyleAttribute(GetStyleName(HtmlTextWriterStyle.Color), "red");
    }
' If the markup element being rendered is a Label,
' render the opening tag of a Font element before it.
If tagKey = HtmlTextWriterTag.Label Then
    ' Check whether a Color style attribute is 
    ' included on the Label. If not, use the
    ' AddStyleAttribute and GetStyleName methods to add one
    ' and set its value to red.
    If Not IsStyleAttributeDefined(HtmlTextWriterStyle.Color) Then
        AddStyleAttribute(GetStyleName(HtmlTextWriterStyle.Color), "red")
    End If

備註

如果 styleKey 不是有效的 HtmlTextWriterStyle 物件,方法會 GetStyleName 傳回空字串 (「」) 。

適用於

另請參閱