HtmlTextWriter.AddAttribute HtmlTextWriter.AddAttribute HtmlTextWriter.AddAttribute HtmlTextWriter.AddAttribute Method

定義

將指定的標記屬性和值加入項目的開頭標記中,這些項目是 HtmlTextWriter 物件經過後續呼叫 RenderBeginTag 方法而建立的。Adds the specified markup attribute and value to the opening tag of the element that the HtmlTextWriter object creates with a subsequent call to the RenderBeginTag method.

多載

AddAttribute(String, String) AddAttribute(String, String) AddAttribute(String, String) AddAttribute(String, String)

將指定的標記屬性和值加入項目的開頭標記中,這些項目是 HtmlTextWriter 物件經過後續呼叫 RenderBeginTag 方法而建立的。Adds the specified markup attribute and value to the opening tag of the element that the HtmlTextWriter object creates with a subsequent call to the RenderBeginTag method.

AddAttribute(HtmlTextWriterAttribute, String) AddAttribute(HtmlTextWriterAttribute, String) AddAttribute(HtmlTextWriterAttribute, String) AddAttribute(HtmlTextWriterAttribute, String)

將標記屬性和屬性值加入項目的開頭標記中,此項目是 HtmlTextWriter 物件經過後續呼叫 RenderBeginTag 方法而建立的。Adds the markup attribute and the attribute value to the opening tag of the element that the HtmlTextWriter object creates with a subsequent call to the RenderBeginTag method.

AddAttribute(String, String, Boolean) AddAttribute(String, String, Boolean) AddAttribute(String, String, Boolean) AddAttribute(String, String, Boolean)

將指定的標記屬性和值加入項目的開頭標記中,這些項目是 HtmlTextWriter 物件經過後續呼叫 RenderBeginTag 方法而建立的 (使用選擇性編碼)。Adds the specified markup attribute and value to the opening tag of the element that the HtmlTextWriter object creates with a subsequent call to the RenderBeginTag method, with optional encoding.

AddAttribute(String, String, HtmlTextWriterAttribute) AddAttribute(String, String, HtmlTextWriterAttribute) AddAttribute(String, String, HtmlTextWriterAttribute) AddAttribute(String, String, HtmlTextWriterAttribute)

將指定的標記屬性和值,連同 HtmlTextWriterAttribute 列舉值,一起加入此項目的開頭標記中,此項目是 HtmlTextWriter 物件經過後續呼叫 RenderBeginTag 方法而建立的。Adds the specified markup attribute and value, along with an HtmlTextWriterAttribute enumeration value, to the opening tag of the element that the HtmlTextWriter object creates with a subsequent call to the RenderBeginTag method.

AddAttribute(HtmlTextWriterAttribute, String, Boolean) AddAttribute(HtmlTextWriterAttribute, String, Boolean) AddAttribute(HtmlTextWriterAttribute, String, Boolean) AddAttribute(HtmlTextWriterAttribute, String, Boolean)

將標記屬性和屬性值加入項目的開頭標記中,此項目是 HtmlTextWriter 物件經過後續呼叫 RenderBeginTag 方法而建立的 (使用選擇性編碼)。Adds the markup attribute and the attribute value to the opening tag of the element that the HtmlTextWriter object creates with a subsequent call to the RenderBeginTag method, with optional encoding.

AddAttribute(String, String) AddAttribute(String, String) AddAttribute(String, String) AddAttribute(String, String)

將指定的標記屬性和值加入項目的開頭標記中,這些項目是 HtmlTextWriter 物件經過後續呼叫 RenderBeginTag 方法而建立的。Adds the specified markup attribute and value to the opening tag of the element that the HtmlTextWriter object creates with a subsequent call to the RenderBeginTag method.

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

字串,包含要加入的屬性名稱。A string containing the name of the attribute to add.

value
String String String String

字串,包含要指派給屬性的值。A string containing the value to assign to the attribute.

範例

下列程式碼範例示範如何在AddAttribute RenderBeginTag呼叫方法之前呼叫方法。The following code example demonstrates how to call the AddAttribute method before calling the RenderBeginTag method. 程式碼會AddAttribute在呼叫RenderBeginTag方法以<span>將元素轉譯為輸出AddStyleAttribute資料流程之前,呼叫的兩個不同多載和兩個不同的多載。The code calls two different overloads of AddAttribute and two different overloads of AddStyleAttribute before it calls the RenderBeginTag method to render a <span> element to the output stream. 方法是用來定義名CustomAttribute為的自訂屬性,以及為此CustomAttributeValue程式碼<span>所轉譯的元素命名的自訂值。 AddAttribute(String, String)The AddAttribute(String, String) method is used to define the custom attribute named CustomAttribute and the custom value named CustomAttributeValue for the <span> element that this code renders.

// 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)一個值,則使用方法的多載,或在執行時間之前不知道該屬性。HtmlTextWriterAttributeUse the AddAttribute overload of the AddAttribute(String, String) method if the attribute is not one of the HtmlTextWriterAttribute values, or if the attribute is not known until run time.

針對任何指定標記專案的實例, HtmlTextWriter類別會維護該元素的屬性清單。For an instance of any given markup element, the HtmlTextWriter class maintains a list of attributes for that element. 呼叫方法時, AddAttribute方法所加入的任何屬性都會轉譯成專案的開頭標記。 RenderBeginTagWhen the RenderBeginTag method is called, any attributes added by the AddAttribute method are rendered to the opening tag of the element. 然後,會從HtmlTextWriter物件中清除屬性清單。The list of attributes is then cleared from the HtmlTextWriter object.

呈現標記元素的編碼模式如下:The coding pattern for rendering markup elements is as follows:

  • AddAttribute使用方法,將任何屬性新增至元素。Use the AddAttribute method to add any attributes to the element.

  • 請使用 RenderBeginTag 方法。Use the RenderBeginTag method.

  • 視需要使用其他方法來轉譯在元素的開頭和結束記號之間找到的內容。Use other methods as needed to render the content found between the element's opening and closing tags.

  • 請使用 RenderEndTag 方法。Use the RenderEndTag method.

另請參閱

AddAttribute(HtmlTextWriterAttribute, String) AddAttribute(HtmlTextWriterAttribute, String) AddAttribute(HtmlTextWriterAttribute, String) AddAttribute(HtmlTextWriterAttribute, String)

將標記屬性和屬性值加入項目的開頭標記中,此項目是 HtmlTextWriter 物件經過後續呼叫 RenderBeginTag 方法而建立的。Adds the markup attribute and the attribute value to the opening tag of the element that the HtmlTextWriter object creates with a subsequent call to the RenderBeginTag method.

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

HtmlTextWriterAttribute,表示要加入輸出資料流的標記屬性。An HtmlTextWriterAttribute that represents the markup attribute to add to the output stream.

value
String String String String

字串,包含要指派給屬性的值。A string containing the value to assign to the attribute.

範例

下列程式碼範例示範如何使用AddAttribute AddAttribute(HtmlTextWriterAttribute, String)方法的多載,將Onclick屬性新增至標記元素,然後將其值設定為下列 ECMAScript 程式碼:The following code example shows how to use the AddAttribute overload of the AddAttribute(HtmlTextWriterAttribute, String) method to add an Onclick attribute to a markup element, and then set its value to the following ECMAScript code:

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

備註

使用AddAttribute(HtmlTextWriterAttribute, String)方法AddAttribute的多載來呈現標準標記屬性。Use the AddAttribute overload of the AddAttribute(HtmlTextWriterAttribute, String) method to render a standard markup attribute.

針對任何指定標記專案的實例, HtmlTextWriter類別會維護該元素的屬性清單。For an instance of any given markup element, the HtmlTextWriter class maintains a list of attributes for that element. 呼叫方法時, AddAttribute方法所加入的任何屬性都會轉譯成專案的開頭標記。 RenderBeginTagWhen the RenderBeginTag method is called, any attributes that are added by the AddAttribute method are rendered to the opening tag of the element. 然後會從中HtmlTextWriter清除屬性清單。The list of attributes is then cleared from the HtmlTextWriter.

呈現標記元素的編碼模式如下:The coding pattern for rendering markup elements is as follows:

  • AddAttribute使用方法,將任何屬性新增至元素。Use the AddAttribute method to add any attributes to the element.

  • 請使用 RenderBeginTag 方法。Use the RenderBeginTag method.

  • 視需要使用其他方法來轉譯在元素的開頭和結束記號之間找到的內容。Use other methods as needed to render the content found between the element's opening and closing tags.

  • 請使用 RenderEndTag 方法。Use the RenderEndTag method.

另請參閱

AddAttribute(String, String, Boolean) AddAttribute(String, String, Boolean) AddAttribute(String, String, Boolean) AddAttribute(String, String, Boolean)

將指定的標記屬性和值加入項目的開頭標記中,這些項目是 HtmlTextWriter 物件經過後續呼叫 RenderBeginTag 方法而建立的 (使用選擇性編碼)。Adds the specified markup attribute and value to the opening tag of the element that the HtmlTextWriter object creates with a subsequent call to the RenderBeginTag method, with optional encoding.

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

字串,包含要加入的屬性名稱。A string containing the name of the attribute to add.

value
String String String String

字串,包含要指派給屬性的值。A string containing the value to assign to the attribute.

fEndode
Boolean Boolean Boolean Boolean

如果要為屬性及屬性值編碼則為 true,否則為 falsetrue to encode the attribute and its value; otherwise, false.

範例

下列程式碼AddAttribute範例示範如何使用AddAttribute(String, String, Boolean)方法的多載,以確保自訂屬性(名為myattribute<img>和其值不會針對元素進行編碼。The following code example shows how to use the AddAttribute overload of the AddAttribute(String, String, Boolean) method to ensure that a custom attribute, named myattribute, and its value are not encoded for an <img> element.

// Control the encoding of attributes.
// Simple known values do not need encoding.
writer->AddAttribute( HtmlTextWriterAttribute::Alt, "Encoding, \"Required\"", true );
writer->AddAttribute( "myattribute", "No &quot;encoding &quot; 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 &quot;encoding &quot; 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 &quot;encoding &quot; required", False)
writer.RenderBeginTag(HtmlTextWriterTag.Img)
writer.RenderEndTag()
writer.WriteLine()

備註

如果屬性AddAttribute不是其中AddAttribute(String, String, Boolean)一個值,則使用方法的多載,或在需要執行時間和編碼之前,屬性是未知的。HtmlTextWriterAttributeUse the AddAttribute overload of the AddAttribute(String, String, Boolean) method if the attribute is not one of the HtmlTextWriterAttribute values, or if the attribute is not known until run time and encoding is needed.

針對任何指定標記專案的實例, HtmlTextWriter類別會維護該元素的屬性清單。For an instance of any given markup element, the HtmlTextWriter class maintains a list of attributes for that element. 呼叫方法時, AddAttribute方法所加入的任何屬性都會轉譯成專案的開頭標記。 RenderBeginTagWhen the RenderBeginTag method is called, any attributes added by the AddAttribute method are rendered to the opening tag of the element. 然後,會從HtmlTextWriter物件中清除屬性清單。The list of attributes is then cleared from the HtmlTextWriter object.

如果屬性可能包含fEncode引號( true")、小於符號(<)或連字號(&),請使用將設定為的方法。AddAttribute(String, String, Boolean)Use the AddAttribute(String, String, Boolean) method with fEncode set to true, if the attribute can possibly contain a quotation mark ("), a less than sign (<), or an ampersand (&). 方法呼叫會將屬性編碼,以符合要求裝置的需求。The method call will encode the attribute to meet the requirements of the requesting device. 如果您知道fEncode這些false字元都不會產生,或如果您知道屬性已經過編碼,您可以將設定為。You can set fEncode to false, if you know that none of these characters will be generated, or if you know that the attribute is already encoded.

如果屬性類型為Style,則fEndode即使為true,也不會編碼值。If the attribute type is Style, the value won't be encoded even if fEndode is true. 請確定樣式值符合 CSS 標準,且不包含惡意程式碼。Ensure that the style value is CSS-compliant and doesn't contain malicious code.

呈現標記元素的編碼模式如下:The coding pattern for rendering markup elements is as follows:

  • AddAttribute使用方法,將任何屬性新增至元素。Use the AddAttribute method to add any attributes to the element.

  • 請使用 RenderBeginTag 方法。Use the RenderBeginTag method.

  • 視需要使用其他方法來轉譯在元素的開頭和結束記號之間找到的內容。Use other methods as needed to render the content found between the element's opening and closing tags.

  • 請使用 RenderEndTag 方法。Use the RenderEndTag method.

另請參閱

AddAttribute(String, String, HtmlTextWriterAttribute) AddAttribute(String, String, HtmlTextWriterAttribute) AddAttribute(String, String, HtmlTextWriterAttribute) AddAttribute(String, String, HtmlTextWriterAttribute)

將指定的標記屬性和值,連同 HtmlTextWriterAttribute 列舉值,一起加入此項目的開頭標記中,此項目是 HtmlTextWriter 物件經過後續呼叫 RenderBeginTag 方法而建立的。Adds the specified markup attribute and value, along with an HtmlTextWriterAttribute enumeration value, to the opening tag of the element that the HtmlTextWriter object creates with a subsequent call to the RenderBeginTag method.

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

字串,包含要加入的屬性名稱。A string containing the name of the attribute to add.

value
String String String String

字串,包含要指派給屬性的值。A string containing the value to assign to the attribute.

備註

只有在AddAttribute繼承自AddAttribute(String, String, HtmlTextWriterAttribute) 類別時,才使用方法的多載。HtmlTextWriterUse the AddAttribute overload of the AddAttribute(String, String, HtmlTextWriterAttribute) method only when inheriting from the HtmlTextWriter class. 它可讓您建立新namekey和成對的屬性。It enables you to create new name and key pairs for attributes.

另請參閱

AddAttribute(HtmlTextWriterAttribute, String, Boolean) AddAttribute(HtmlTextWriterAttribute, String, Boolean) AddAttribute(HtmlTextWriterAttribute, String, Boolean) AddAttribute(HtmlTextWriterAttribute, String, Boolean)

將標記屬性和屬性值加入項目的開頭標記中,此項目是 HtmlTextWriter 物件經過後續呼叫 RenderBeginTag 方法而建立的 (使用選擇性編碼)。Adds the markup attribute and the attribute value to the opening tag of the element that the HtmlTextWriter object creates with a subsequent call to the RenderBeginTag method, with optional encoding.

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

HtmlTextWriterAttribute,表示要加入輸出資料流的標記屬性。An HtmlTextWriterAttribute that represents the markup attribute to add to the output stream.

value
String String String String

字串,包含要指派給屬性的值。A string containing the value to assign to the attribute.

fEncode
Boolean Boolean Boolean Boolean

如果要為屬性及屬性值編碼則為 true,否則為 falsetrue to encode the attribute and its value; otherwise, false.

範例

下列程式碼範例示範如何使用AddAttribute(HtmlTextWriterAttribute, String, Boolean)方法,以確保指派Alt給專案之屬性<img>的字串值已針對要求的裝置進行編碼。The following code example demonstrates how to use the AddAttribute(HtmlTextWriterAttribute, String, Boolean) method to ensure that the string value that is assigned to an Alt attribute for an <img> element is encoded for the requesting device.

// Control the encoding of attributes.
// Simple known values do not need encoding.
writer->AddAttribute( HtmlTextWriterAttribute::Alt, "Encoding, \"Required\"", true );
writer->AddAttribute( "myattribute", "No &quot;encoding &quot; 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 &quot;encoding &quot; 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 &quot;encoding &quot; required", False)
writer.RenderBeginTag(HtmlTextWriterTag.Img)
writer.RenderEndTag()
writer.WriteLine()

備註

使用AddAttribute(HtmlTextWriterAttribute, String, Boolean)方法AddAttribute的多載,以選擇性的編碼來呈現標準標記屬性。Use the AddAttribute overload of the AddAttribute(HtmlTextWriterAttribute, String, Boolean) method to render a standard markup attribute, with optional encoding.

針對任何指定標記專案的實例, HtmlTextWriter類別會維護該元素的屬性清單。For an instance of any given markup element, the HtmlTextWriter class maintains a list of attributes for that element. 呼叫方法時,會將所新增的AddAttribute任何屬性轉譯為專案的開頭標記。 RenderBeginTagWhen the RenderBeginTag method is called, any attributes added by the AddAttribute are rendered to the opening tag of the element. 然後,會從HtmlTextWriter物件中清除屬性清單。The list of attributes is then cleared from the HtmlTextWriter object.

如果屬性可能包含fEncode引號( true")、小於符號(<)或連字號(&),請使用將設定為的方法。AddAttribute(HtmlTextWriterAttribute, String, Boolean)Use the AddAttribute(HtmlTextWriterAttribute, String, Boolean) method with fEncode set to true, if the attribute can possibly contain a quotation mark ("), a less than sign (<), or an ampersand (&). 方法呼叫會將屬性編碼,以符合要求裝置的需求。The method call will encode the attribute to meet the requirements of the requesting device. 如果您知道fEncode這些false字元都不會產生,或如果您知道屬性已經過編碼,您可以將設定為。You can set fEncode to false, if you know that none of these characters will be generated, or if you know that the attribute is already encoded.

如果屬性類型為Style,則fEncode即使為true,也不會編碼值。If the attribute type is Style, the value won't be encoded even if fEncode is true. 請確定樣式值符合 CSS 標準,且不包含惡意程式碼。Ensure that the style value is CSS-compliant and doesn't contain malicious code.

呈現標記元素的編碼模式如下:The coding pattern for rendering markup elements is as follows:

  • AddAttribute使用方法,將任何屬性新增至元素。Use the AddAttribute method to add any attributes to the element.

  • 請使用 RenderBeginTag 方法。Use the RenderBeginTag method.

  • 視需要使用其他方法來轉譯在元素的開頭和結束記號之間找到的內容。Use other methods as needed to render the content found between the element's opening and closing tags.

  • 請使用 RenderEndTag 方法。Use the RenderEndTag method.

另請參閱

適用於