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

返回

String

styleKey 中指定的 HtmlTextWriterStyle 枚举值相关联的样式属性名称。

示例

下面的代码示例演示如何检查是否 <label> 正在呈现元素。 如果是这样,它会检查 Color 是否已在元素上定义样式属性。 如果未定义该属性, AddStyleAttribute 将调用该方法,并且 GetStyleName 该方法 Color 会将枚举成员转换为其字符串表示形式,以传递给 AddStyleAttribute 该方法。

// 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 将返回空字符串 (“”) 。

适用于

另请参阅