HtmlTextWriter.GetStyleName(HtmlTextWriterStyle) HtmlTextWriter.GetStyleName(HtmlTextWriterStyle) HtmlTextWriter.GetStyleName(HtmlTextWriterStyle) HtmlTextWriter.GetStyleName(HtmlTextWriterStyle) Method

定義

取得與指定的 HtmlTextWriterStyle 列舉值相關聯的標記樣式屬性名稱。Obtains the markup style attribute name associated with the specified HtmlTextWriterStyle enumeration value.

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 HtmlTextWriterStyle HtmlTextWriterStyle

要取得的樣式屬性名稱之 HtmlTextWriterStyleThe HtmlTextWriterStyle to obtain the style attribute name for.

傳回

styleKey 中所指定的 HtmlTextWriterStyle 列舉值相關聯的樣式屬性名稱。The style attribute name associated with the HtmlTextWriterStyle enumeration value specified in styleKey.

範例

下列程式碼範例示範如何檢查是否<label>正在呈現專案。The following code example shows how to check whether a <label> element is being rendered. 若是如此,它會檢查是否Color已在元素上定義樣式屬性。If so, it checks whether a Color style attribute has been defined on the element. 如果尚未定義屬性, AddStyleAttribute則會呼叫方法, GetStyleName而方法會將Color列舉成員轉換AddStyleAttribute成其字串表示,以傳遞至方法。If the attribute has not been defined, the AddStyleAttribute method is called and the GetStyleName method converts the Color enumeration member into its string representation to pass to the AddStyleAttribute method.

// 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方法會傳回空字串("")。If styleKey is not a valid HtmlTextWriterStyle object, the GetStyleName method returns an empty string ("").

適用於

另請參閱