HtmlTextWriter.AddAttribute 方法

定義

將指定的標記屬性與值加入物件在後續呼叫HtmlTextWriter方法時所建立的元素RenderBeginTag開頭標籤。

多載

名稱 Description
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

一個包含要指派給屬性的值的字串。

範例

以下程式碼範例示範如何在呼叫AddAttribute方法之前先呼叫該RenderBeginTag方法。 程式碼在呼叫AddAttribute將元素渲染AddStyleAttribute到輸出串流之前,先呼叫兩個不同的超載和兩個不同的超載RenderBeginTag<span>。 此AddAttribute(String, String)方法用於定義該程式碼所渲染元素所命名CustomAttribute的自訂屬性與自訂CustomAttributeValue<span>值。

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

備註

如果屬性不是其中AddAttribute的值,或該屬性直到執行時才知道,則使用AddAttribute(String, String)方法的超載HtmlTextWriterAttribute

對於任一標記元素的實例,類別 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 方法的 AddAttribute(HtmlTextWriterAttribute, String) 超載功能,為標記元素新增 Onclick 屬性,並將其值設定為以下 ECMAScript 程式碼:

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

備註

利用 AddAttribute 方法的 AddAttribute(HtmlTextWriterAttribute, String) 超載來渲染一個標準標記屬性。

對於任一標記元素的實例,類別 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 方法的 AddAttribute(String, String, Boolean) 超載,確保一個名為 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()

備註

如果該屬性不是值之一AddAttribute,或該屬性直到執行時才知道,且需要編碼時,則使用AddAttribute(String, String, Boolean)方法的超載HtmlTextWriterAttribute值。

對於任一標記元素的實例,類別 HtmlTextWriter 會維護該元素的屬性清單。 當呼叫該 RenderBeginTag 方法時,該方法新增 AddAttribute 的任何屬性都會被渲染成該元素的開頭標籤。 接著,物件中的屬性清單會被清除 HtmlTextWriter

若屬性可能包含引號(“)、小於符號的(AddAttribute(String, String, Boolean))、或與號(&”),則使用fEncodetrue<為 的方法。 方法呼叫會將屬性編碼以符合請求裝置的需求。 如果你知道這些字元都不會被產生,或該屬性已經編碼好,你可以將 設 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

一個包含要指派給屬性的值的字串。

key
HtmlTextWriterAttribute

一個 HtmlTextWriterAttribute 代表屬性的 。

備註

只有在從類別繼AddAttribute承時才使用AddAttribute(String, String, HtmlTextWriterAttribute)方法的HtmlTextWriter超載。 它讓你能為屬性建立新和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) 方法,確保分配給 Alt 元素屬性 <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()

備註

利用 AddAttribute 方法的 AddAttribute(HtmlTextWriterAttribute, String, Boolean) 超載來渲染標準標記屬性,並可選擇編碼。

對於任一標記元素的實例,類別 HtmlTextWriter 會維護該元素的屬性清單。 當呼叫該 RenderBeginTag 方法時,所新增 AddAttribute 的任何屬性都會被渲染到元素的開頭標籤中。 接著,物件中的屬性清單會被清除 HtmlTextWriter

若屬性可能包含引號(“)、小於符號的(AddAttribute(HtmlTextWriterAttribute, String, Boolean))、或與號(&”),則使用fEncodetrue<為 的方法。 方法呼叫會將屬性編碼以符合請求裝置的需求。 如果你知道這些字元都不會被產生,或該屬性已經編碼好,你可以將 設 fEncodefalse

若屬性類型為 Style,即使 fEncodetrue,該值也不會被編碼。 確保樣式值符合 CSS 規範且不含惡意程式碼。

標記元素的渲染編碼模式如下:

  • 使用該 AddAttribute 方法為元素添加任何屬性。

  • 使用 RenderBeginTag 方法。

  • 根據需要,使用其他方法來渲染元素開門與關閉標籤之間的內容。

  • 使用 RenderEndTag 方法。

另請參閱

適用於