HtmlTextWriter.AddAttribute メソッド

定義

HtmlTextWriter オブジェクトが作成する要素の開始タグに、指定したマークアップ属性と値を追加し、RenderBeginTag メソッドを後続の呼び出しで呼び出します。

オーバーロード

名前 説明
AddAttribute(String, String)

HtmlTextWriter オブジェクトが作成する要素の開始タグに、指定したマークアップ属性と値を追加し、RenderBeginTag メソッドを後続の呼び出しで呼び出します。

AddAttribute(HtmlTextWriterAttribute, String)

HtmlTextWriter メソッドの後続の呼び出しで、RenderBeginTag オブジェクトが作成する要素の開始タグにマークアップ属性と属性値を追加します。

AddAttribute(String, String, Boolean)

HtmlTextWriter オブジェクトが後で RenderBeginTag メソッドを呼び出して作成する要素の開始タグに、指定したマークアップ属性と値を追加します (省略可能なエンコード)。

AddAttribute(String, String, HtmlTextWriterAttribute)

指定したマークアップの属性と値を、 HtmlTextWriterAttribute 列挙値と共に、 HtmlTextWriter オブジェクトが作成する要素の開始タグに追加し、 RenderBeginTag メソッドを後続の呼び出しで呼び出します。

AddAttribute(HtmlTextWriterAttribute, String, Boolean)

マークアップ属性と属性値を、 HtmlTextWriter オブジェクトが後で RenderBeginTag メソッドを呼び出して作成する要素の開始タグに追加します (省略可能なエンコード)。

AddAttribute(String, String)

HtmlTextWriter オブジェクトが作成する要素の開始タグに、指定したマークアップ属性と値を追加し、RenderBeginTag メソッドを後続の呼び出しで呼び出します。

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

パラメーター

name
String

追加する属性の名前を含む文字列。

value
String

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

次のコード例では、RenderBeginTag メソッドを呼び出す前に、AddAttribute メソッドを呼び出す方法を示します。 コードは、 AddAttribute の 2 つの異なるオーバーロードと 2 つの異なる AddStyleAttribute オーバーロードを呼び出してから、 RenderBeginTag メソッドを呼び出して <span> 要素を出力ストリームにレンダリングします。 AddAttribute(String, String) メソッドは、CustomAttributeという名前のカスタム属性と、このコードでレンダリングされる<span>要素のCustomAttributeValueという名前のカスタム値を定義するために使用されます。

// Set attributes and values along with attributes and styles  
// attribute defined for a <span> element.
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "alert('Hello');");
writer.AddAttribute("CustomAttribute", "CustomAttributeValue");
writer.AddStyleAttribute(HtmlTextWriterStyle.Color, "Red");
writer.AddStyleAttribute("Customstyle", "CustomStyleValue");
writer.RenderBeginTag(HtmlTextWriterTag.Span);
// Create a space and indent the markup inside the 
// <span> element.
writer.WriteLine();
writer.Indent++;
' Set attributes and values along with attributes and styles
' attribute defined for a <span> element.
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "alert('Hello');")
writer.AddAttribute("CustomAttribute", "CustomAttributeValue")
writer.AddStyleAttribute(HtmlTextWriterStyle.Color, "Red")
writer.AddStyleAttribute("CustomStyle", "CustomStyleValue")
writer.RenderBeginTag(HtmlTextWriterTag.Span)

'  Create a space and indent the markup inside the 
' <span> element.
writer.WriteLine()
writer.Indent += 1

注釈

属性がHtmlTextWriterAttribute値の 1 つでない場合、または実行時まで属性が不明な場合は、AddAttribute(String, String) メソッドのAddAttribute オーバーロードを使用します。

特定のマークアップ要素のインスタンスの場合、 HtmlTextWriter クラスはその要素の属性のリストを保持します。 RenderBeginTag メソッドが呼び出されると、AddAttribute メソッドによって追加されたすべての属性が要素の開始タグにレンダリングされます。 その後、 HtmlTextWriter オブジェクトから属性の一覧がクリアされます。

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

  • 要素に属性を追加するには、 AddAttribute メソッドを使用します。

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

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

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

こちらもご覧ください

適用対象

AddAttribute(HtmlTextWriterAttribute, String)

HtmlTextWriter メソッドの後続の呼び出しで、RenderBeginTag オブジェクトが作成する要素の開始タグにマークアップ属性と属性値を追加します。

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

パラメーター

key
HtmlTextWriterAttribute

出力ストリームに追加するマークアップ属性を表す HtmlTextWriterAttribute

value
String

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

次のコード例は、AddAttribute(HtmlTextWriterAttribute, String) メソッドのAddAttribute オーバーロードを使用してマークアップ要素にOnclick属性を追加し、その値を次の ECMAScript コードに設定する方法を示しています。

alert('Hello');
writer->AddAttribute( HtmlTextWriterAttribute::Onclick, "alert('Hello');" );
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "alert('Hello');");
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "alert('Hello');")

注釈

標準のマークアップ属性をレンダリングするには、AddAttribute(HtmlTextWriterAttribute, String) メソッドのAddAttributeオーバーロードを使用します。

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

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

  • 要素に属性を追加するには、 AddAttribute メソッドを使用します。

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

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

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

こちらもご覧ください

適用対象

AddAttribute(String, String, Boolean)

HtmlTextWriter オブジェクトが後で RenderBeginTag メソッドを呼び出して作成する要素の開始タグに、指定したマークアップ属性と値を追加します (省略可能なエンコード)。

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

パラメーター

name
String

追加する属性の名前を含む文字列。

value
String

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

fEndode
Boolean

true 属性とその値をエンコードする場合。それ以外の場合は false

次のコード例は、AddAttribute(String, String, Boolean) メソッドのAddAttribute オーバーロードを使用して、myattributeという名前のカスタム属性とその値が<img>要素に対してエンコードされないようにする方法を示しています。

// Control the encoding of attributes.
// Simple known values do not need encoding.
writer->AddAttribute( HtmlTextWriterAttribute::Alt, "Encoding, \"Required\"", true );
writer->AddAttribute( "myattribute", "No "encoding " required", false );
writer->RenderBeginTag( HtmlTextWriterTag::Img );
writer->RenderEndTag();
writer->WriteLine();
// Control the encoding of attributes. 
// Simple known values do not need encoding.
writer.AddAttribute(HtmlTextWriterAttribute.Alt, "Encoding, \"Required\"", true);
writer.AddAttribute("myattribute", "No "encoding " required", false);
writer.RenderBeginTag(HtmlTextWriterTag.Img);
writer.RenderEndTag();
writer.WriteLine();
' Control the encoding of attributes.
' Simple known values do not need encoding.
writer.AddAttribute(HtmlTextWriterAttribute.Alt, "Encoding, ""Required""", True)
writer.AddAttribute("myattribute", "No "encoding " required", False)
writer.RenderBeginTag(HtmlTextWriterTag.Img)
writer.RenderEndTag()
writer.WriteLine()

注釈

属性がHtmlTextWriterAttribute値の 1 つでない場合、または実行時とエンコードが必要になるまで属性が不明な場合は、AddAttribute(String, String, Boolean) メソッドのAddAttribute オーバーロードを使用します。

特定のマークアップ要素のインスタンスの場合、 HtmlTextWriter クラスはその要素の属性のリストを保持します。 RenderBeginTag メソッドが呼び出されると、AddAttribute メソッドによって追加されたすべての属性が要素の開始タグにレンダリングされます。 その後、 HtmlTextWriter オブジェクトから属性の一覧がクリアされます。

属性に引用符 (")、小さい符号 (<)、またはアンパサンド (>) を含めることができる場合は、fEncodetrue に設定したAddAttribute(String, String, Boolean) メソッドを使用します。 メソッド呼び出しは、要求するデバイスの要件を満たすために属性をエンコードします。 これらの文字が生成されないことがわかっている場合、または属性が既にエンコードされていることがわかっている場合は、 fEncodefalse に設定できます。

属性の型が Style場合、 fEndodetrueされている場合でも、値はエンコードされません。 スタイル値が CSS に準拠しており、悪意のあるコードが含まれていないことを確認します。

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

  • 要素に属性を追加するには、 AddAttribute メソッドを使用します。

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

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

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

こちらもご覧ください

適用対象

AddAttribute(String, String, HtmlTextWriterAttribute)

指定したマークアップの属性と値を、 HtmlTextWriterAttribute 列挙値と共に、 HtmlTextWriter オブジェクトが作成する要素の開始タグに追加し、 RenderBeginTag メソッドを後続の呼び出しで呼び出します。

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

パラメーター

name
String

追加する属性の名前を含む文字列。

value
String

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

注釈

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

こちらもご覧ください

適用対象

AddAttribute(HtmlTextWriterAttribute, String, Boolean)

マークアップ属性と属性値を、 HtmlTextWriter オブジェクトが後で RenderBeginTag メソッドを呼び出して作成する要素の開始タグに追加します (省略可能なエンコード)。

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

パラメーター

key
HtmlTextWriterAttribute

出力ストリームに追加するマークアップ属性を表す HtmlTextWriterAttribute

value
String

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

fEncode
Boolean

true 属性とその値をエンコードする場合。それ以外の場合は false

次のコード例では、AddAttribute(HtmlTextWriterAttribute, String, Boolean) メソッドを使用して、<img>要素のAlt属性に割り当てられた文字列値が要求側デバイスにエンコードされるようにする方法を示します。

// Control the encoding of attributes.
// Simple known values do not need encoding.
writer->AddAttribute( HtmlTextWriterAttribute::Alt, "Encoding, \"Required\"", true );
writer->AddAttribute( "myattribute", "No "encoding " required", false );
writer->RenderBeginTag( HtmlTextWriterTag::Img );
writer->RenderEndTag();
writer->WriteLine();
// Control the encoding of attributes. 
// Simple known values do not need encoding.
writer.AddAttribute(HtmlTextWriterAttribute.Alt, "Encoding, \"Required\"", true);
writer.AddAttribute("myattribute", "No "encoding " required", false);
writer.RenderBeginTag(HtmlTextWriterTag.Img);
writer.RenderEndTag();
writer.WriteLine();
' Control the encoding of attributes.
' Simple known values do not need encoding.
writer.AddAttribute(HtmlTextWriterAttribute.Alt, "Encoding, ""Required""", True)
writer.AddAttribute("myattribute", "No "encoding " required", False)
writer.RenderBeginTag(HtmlTextWriterTag.Img)
writer.RenderEndTag()
writer.WriteLine()

注釈

AddAttribute(HtmlTextWriterAttribute, String, Boolean) メソッドのAddAttribute オーバーロードを使用して、省略可能なエンコードで標準マークアップ属性をレンダリングします。

特定のマークアップ要素のインスタンスの場合、 HtmlTextWriter クラスはその要素の属性のリストを保持します。 RenderBeginTag メソッドが呼び出されると、AddAttributeによって追加されたすべての属性が要素の開始タグにレンダリングされます。 その後、 HtmlTextWriter オブジェクトから属性の一覧がクリアされます。

属性に引用符 (")、小さい符号 (<)、またはアンパサンド (>) を含めることができる場合は、fEncodetrue に設定したAddAttribute(HtmlTextWriterAttribute, String, Boolean) メソッドを使用します。 メソッド呼び出しは、要求するデバイスの要件を満たすために属性をエンコードします。 これらの文字が生成されないことがわかっている場合、または属性が既にエンコードされていることがわかっている場合は、 fEncodefalse に設定できます。

属性の型が Style場合、 fEncodetrueされている場合でも、値はエンコードされません。 スタイル値が CSS に準拠しており、悪意のあるコードが含まれていないことを確認します。

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

  • 要素に属性を追加するには、 AddAttribute メソッドを使用します。

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

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

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

こちらもご覧ください

適用対象