HtmlTextWriter.GetStyleKey(String) HtmlTextWriter.GetStyleKey(String) HtmlTextWriter.GetStyleKey(String) HtmlTextWriter.GetStyleKey(String) Method

定義

指定したスタイルの HtmlTextWriterStyle 列挙値を取得します。Obtains the HtmlTextWriterStyle enumeration value for the specified style.

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

HtmlTextWriterStyle の取得対象のスタイル属性。The style attribute for which to obtain the HtmlTextWriterStyle.

戻り値

styleName に対応する HtmlTextWriterStyle 列挙値。The HtmlTextWriterStyle enumeration value corresponding to styleName.

クラスから派生したクラスのRenderBeginTagメソッドをオーバーライドする方法を次のコード例に示します。 HtmlTextWriterThe following code example demonstrates how to override the RenderBeginTag method in a class derived from the HtmlTextWriter class. オーバーライドは、マークアップ<label>を表示するかどうかを決定し、存在する場合は、 Color要素に属性があるかどうかをチェックします。 RenderBeginTagThe RenderBeginTag override determines whether a <label> markup will be rendered, and, if so, checks the element for a Color attribute. Color AddStyleAttribute GetStyleKey Color <label>属性が定義されていない場合は、メソッドを呼び出してマークアップ要素に属性を追加し、属性をに設定するための最初のパラメーターとしてメソッドを使用します。 Color red.If a Color attribute has not been defined, the GetStyleKey method is used as the first parameter in a call to the AddStyleAttribute method to add a Color attribute to a <label> markup element and set the Color attribute to red.

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

注釈

GetStyleKey 列挙値にHtmlTextWriterStyle対応していない場合HtmlTextWriterStyle styleName 、メソッドは値-1 を値として返します。The GetStyleKey method returns the value -1 typed as a HtmlTextWriterStyle value, if styleName does not correspond to any HtmlTextWriterStyle enumeration value.

適用対象

こちらもご覧ください