HtmlTextWriter.AddStyleAttribute メソッド

定義

RenderBeginTag メソッドの後続の呼び出しで、HtmlTextWriter オブジェクトによって作成される要素の開始タグにマークアップ スタイル属性を追加します。

オーバーロード

名前 説明
AddStyleAttribute(String, String)

指定したマークアップ スタイル属性と属性値を、後続の RenderBeginTag メソッドの呼び出しによって作成された開始マークアップ タグに追加します。

AddStyleAttribute(HtmlTextWriterStyle, String)

指定した HtmlTextWriterStyle 値に関連付けられたマークアップ スタイル属性と属性値を、後続の RenderBeginTag メソッドの呼び出しによって作成された開始マークアップ タグに追加します。

AddStyleAttribute(String, String, HtmlTextWriterStyle)

指定したマークアップ スタイル属性と属性値を、 HtmlTextWriterStyle 列挙値と共に、 RenderBeginTag メソッドの後続の呼び出しによって作成された開始マークアップ タグに追加します。

AddStyleAttribute(String, String)

指定したマークアップ スタイル属性と属性値を、後続の RenderBeginTag メソッドの呼び出しによって作成された開始マークアップ タグに追加します。

public:
 virtual void AddStyleAttribute(System::String ^ name, System::String ^ value);
public virtual void AddStyleAttribute(string name, string value);
abstract member AddStyleAttribute : string * string -> unit
override this.AddStyleAttribute : string * string -> unit
Public Overridable Sub AddStyleAttribute (name As String, value As String)

パラメーター

name
String

追加するスタイル属性を含む文字列。

value
String

属性に割り当てる値を含む文字列。

次のコード例は、AddStyleAttribute(String, String) メソッドのRenderBeginTag オーバーロードを使用して、<p>要素にfont-sizeおよびcolorスタイル属性をレンダリングする方法を示しています。 このコード例では、 HtmlTextWriter クラスを使用してコントロールの内容をレンダリングします。

// Add style attribute for 'p'(paragraph) element.
writer->AddStyleAttribute( "font-size", "12pt" );
writer->AddStyleAttribute( "color", "fuchsia" );
// Output the 'p' (paragraph) element with the style attributes.
writer->RenderBeginTag( "p" );
// Output the 'Message' property contents and the time on the server.
writer->Write( String::Concat( Message, "<br>",
   "The time on the server: ",
   System::DateTime::Now.ToLongTimeString() ) );

// Close the element.
writer->RenderEndTag();
// Add style attribute for 'p'(paragraph) element.
writer.AddStyleAttribute("font-size", "12pt");
writer.AddStyleAttribute("color", "fuchsia");
// Output the 'p' (paragraph) element with the style attributes.
writer.RenderBeginTag("p");
// Output the 'Message' property contents and the time on the server.
writer.Write(Message + "<br>" +
    "The time on the server: " +
    System.DateTime.Now.ToLongTimeString());

// Close the element.
writer.RenderEndTag();
'Add style attribute for 'p'(paragraph) element.
writer.AddStyleAttribute("font-size", "12pt")
writer.AddStyleAttribute("color", "fuchsia")

'Output the 'p' (paragraph) element with the style attributes.
writer.RenderBeginTag("p")

'Output the 'Message' property contents and the time on the server.
writer.Write((Message & "<br>" & "The time on the server: " & _
   System.DateTime.Now.ToLongTimeString()))

' Close the element.
writer.RenderEndTag()

注釈

スタイルがHtmlTextWriterStyle列挙体のメンバーでない場合、または実行時まで不明な場合は、AddStyleAttribute(String, String) メソッドのAddStyleAttribute オーバーロードを使用します。

HtmlTextWriter クラスは、レンダリングするマークアップ要素のスタイルの一覧を保持します。 RenderBeginTag メソッドが呼び出されると、AddStyleAttribute メソッドによって追加されたすべてのスタイルが、要素の開始タグにレンダリングされます。 その後、スタイルの一覧がクリアされます。

マークアップ要素をレンダリングするためのコーディング パターンは次のとおりです。

  • AddStyleAttribute メソッドを使用して、要素にスタイル属性を追加します。

  • RenderBeginTag メソッドを使用します。

  • 必要に応じて他のメソッドを使用して、要素の開始タグと終了タグの間で見つかったコンテンツをレンダリングします。

  • RenderEndTag メソッドを使用します。

こちらもご覧ください

適用対象

AddStyleAttribute(HtmlTextWriterStyle, String)

指定した HtmlTextWriterStyle 値に関連付けられたマークアップ スタイル属性と属性値を、後続の RenderBeginTag メソッドの呼び出しによって作成された開始マークアップ タグに追加します。

public:
 virtual void AddStyleAttribute(System::Web::UI::HtmlTextWriterStyle key, System::String ^ value);
public virtual void AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle key, string value);
abstract member AddStyleAttribute : System.Web.UI.HtmlTextWriterStyle * string -> unit
override this.AddStyleAttribute : System.Web.UI.HtmlTextWriterStyle * string -> unit
Public Overridable Sub AddStyleAttribute (key As HtmlTextWriterStyle, value As String)

パラメーター

key
HtmlTextWriterStyle

出力ストリームに追加するスタイル属性を表す HtmlTextWriterStyle

value
String

属性に割り当てる値を含む文字列。

次のコード例は、HtmlTextWriter クラスから派生したクラスで、RenderBeginTag メソッドのオーバーライドの一部を使用する方法を示しています。 コードは、 <Label> 要素がレンダリングされているかどうかを確認します。 その場合は、Color スタイル属性が <Label> 要素に対して定義されているかどうかを確認するために、IsStyleAttributeDefined メソッドが呼び出されます。 Color属性が定義されていない場合、コードはAddStyleAttribute メソッドのこのオーバーロードを呼び出してColor属性を style 属性に追加し、その値を 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)
{
    // 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

注釈

スタイルがHtmlTextWriterStyle列挙体のメンバーであり、実行時より前に認識されている場合は、AddStyleAttribute(HtmlTextWriterStyle, String) メソッドのAddStyleAttribute オーバーロードを使用します。

HtmlTextWriter クラスは、レンダリングするマークアップ要素のスタイルの一覧を保持します。 RenderBeginTag メソッドが呼び出されると、AddStyleAttribute メソッドによって追加されたすべてのスタイルが要素の開始タグにレンダリングされます。 その後、スタイルの一覧がクリアされます。

マークアップ要素をレンダリングするためのコーディング パターンは次のとおりです。

  • AddStyleAttribute メソッドを使用して、要素にスタイル属性を追加します。

  • RenderBeginTag メソッドを使用します。

  • 必要に応じて他のメソッドを使用して、要素の開始タグと終了タグの間で見つかったコンテンツをレンダリングします。

  • RenderEndTag メソッドを使用します。

こちらもご覧ください

適用対象

AddStyleAttribute(String, String, HtmlTextWriterStyle)

指定したマークアップ スタイル属性と属性値を、 HtmlTextWriterStyle 列挙値と共に、 RenderBeginTag メソッドの後続の呼び出しによって作成された開始マークアップ タグに追加します。

protected:
 virtual void AddStyleAttribute(System::String ^ name, System::String ^ value, System::Web::UI::HtmlTextWriterStyle key);
protected virtual void AddStyleAttribute(string name, string value, System.Web.UI.HtmlTextWriterStyle key);
abstract member AddStyleAttribute : string * string * System.Web.UI.HtmlTextWriterStyle -> unit
override this.AddStyleAttribute : string * string * System.Web.UI.HtmlTextWriterStyle -> unit
Protected Overridable Sub AddStyleAttribute (name As String, value As String, key As HtmlTextWriterStyle)

パラメーター

name
String

追加するスタイル属性を含む文字列。

value
String

属性に割り当てる値を含む文字列。

key
HtmlTextWriterStyle

追加するスタイル属性を表す HtmlTextWriterStyle

注釈

HtmlTextWriter クラスから継承する場合にのみ、AddStyleAttribute(String, String, HtmlTextWriterStyle) メソッドのAddStyleAttribute オーバーロードを使用します。 これにより、HtmlTextWriterStyle属性の新しいnamevalueのペアを作成できます。

こちらもご覧ください

適用対象