HtmlTextWriter.GetStyleName(HtmlTextWriterStyle) 方法

定义

获取与指定的 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

要获取其样式属性名称的 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 ("").

适用于

另请参阅