次の方法で共有


HtmlTextWriter.AddStyleAttribute メソッド (HtmlTextWriterStyle, String)

key パラメータで指定した HtmlTextWriterStyle 値に関連付けられた HTML スタイル属性とその属性の値を HtmlTextWriter 出力ストリームに追加します。

Overloads Public Overridable Sub AddStyleAttribute( _
   ByVal key As HtmlTextWriterStyle, _   ByVal value As String _)
[C#]
public virtual void AddStyleAttribute(HtmlTextWriterStylekey,stringvalue);
[C++]
public: virtual void AddStyleAttribute(HtmlTextWriterStylekey,String* value);
[JScript]
public function AddStyleAttribute(
   key : HtmlTextWriterStyle,value : String);

パラメータ

  • key
    追加する HTML スタイル属性を表す HtmlTextWriterStyle 値。
  • value
    HTML 属性に代入する値。

解説

スタイルが HtmlTextWriterStyle 列挙値のメンバで、実行時の前に認識される場合は、このバージョンの AddStyleAttribute メソッドを使用します。

このメソッドは、 AddAttribute メソッドに類似していますが、 Style 属性に個別の HTML スタイルを追加します。

HTML 要素のインスタンスの場合、 HtmlTextWriter クラスはその要素のスタイルのリストを維持します。 RenderBeginTag メソッドが呼び出されると、このメソッドで追加されたすべてのスタイルが HTML 要素の開始タグに表示されます。スタイルのリストはその後消去されます。

HTML 要素を表示する通常のパターンでは、このメソッドを使用して属性を要素に追加し、 RenderBeginTag を呼び出し、要素の開始タグと終了タグの間にある内容を表示してから、 RenderEndTag メソッドを呼び出します。

通常、このメソッドは、カスタム サーバー コントロールを作成するときに呼び出します。

使用例

[Visual Basic, C#, C++] AddStyleAttribute メソッドを使用して Color 属性を HTML スタイル属性に追加し、その値に Red を設定する例を次に示します。

 
' If the HTML 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

[C#] 
// If the HTML 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");
  }

[C++] 
// If the HTML 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), S"red");
   }

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 2000, Windows XP Professional, Windows Server 2003 ファミリ

参照

HtmlTextWriter クラス | HtmlTextWriter メンバ | System.Web.UI 名前空間 | HtmlTextWriter.AddStyleAttribute オーバーロードの一覧