次の方法で共有


HtmlTextWriter.GetStyleKey メソッド

指定したスタイルの HtmlTextWriterStyle 列挙値を取得します。

名前空間: System.Web.UI
アセンブリ: System.Web (system.web.dll 内)

構文

'宣言
Protected Function GetStyleKey ( _
    styleName As String _
) As HtmlTextWriterStyle
'使用
Dim styleName As String
Dim returnValue As HtmlTextWriterStyle

returnValue = Me.GetStyleKey(styleName)
protected HtmlTextWriterStyle GetStyleKey (
    string styleName
)
protected:
HtmlTextWriterStyle GetStyleKey (
    String^ styleName
)
protected HtmlTextWriterStyle GetStyleKey (
    String styleName
)
protected function GetStyleKey (
    styleName : String
) : HtmlTextWriterStyle
適用できません。

パラメータ

戻り値

styleName に対応する HtmlTextWriterStyle 列挙値。

解説

styleName がどの HtmlTextWriterStyle 属性値にも対応しない場合、GetStyleKey メソッドは、HtmlTextWriterStyle 値として型指定された値 -1 を返します。

使用例

HtmlTextWriter から派生したクラスの RenderBeginTag メソッドをオーバーライドする方法を次のコード例に示します。RenderBeginTag オーバーライドは、<label> マークアップを出力するかどうかを決定します。出力する場合は、Color 属性の要素をチェックします。Color 属性が定義されていない場合は、AddStyleAttribute メソッド呼び出しの 1 番目のパラメータとして GetStyleKey メソッドを使用して、Color 属性を <label> マークアップ要素に追加し、Color 属性を red に設定します。

' 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
// 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.   
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 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 call adds a Color attribute
    // and sets it to red.
    if (tagKey.Equals(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 call adds a size attribute
    // and sets it to 30 point. 
    if (tagKey.Equals(HtmlTextWriterTag.Font)) {
        if (!(IsAttributeDefined(HtmlTextWriterAttribute.Size))) {
            AddAttribute(GetAttributeKey("size"), "30pt");
        }
    }

    // Call the base class's RenderBeginTag method
    // to ensure that calling this custom HtmlTextWriter
    // includes functionality for all other HTML elements.
    super.RenderBeginTag(tagKey);
} //RenderBeginTag

プラットフォーム

Windows 98,Windows Server 2000 SP4,Windows CE,Windows Millennium Edition,Windows Mobile for Pocket PC,Windows Mobile for Smartphone,Windows Server 2003,Windows XP Media Center Edition,Windows XP Professional x64 Edition,Windows XP SP2,Windows XP Starter Edition

Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。

バージョン情報

.NET Framework

サポート対象 : 3.0,2.0,1.1,1.0

参照

関連項目

HtmlTextWriter クラス
HtmlTextWriter メンバ
System.Web.UI 名前空間
TextWriter
HtmlTextWriterStyle
AddStyleAttribute