Szerkesztés

Megosztás a következőn keresztül:


HtmlTextWriter.AddAttribute Method

Definition

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.

Overloads

AddAttribute(String, String)

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)

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)

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)

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)

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)

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)

Parameters

name
String

A string containing the name of the attribute to add.

value
String

A string containing the value to assign to the attribute.

Examples

The following code example demonstrates how to call the AddAttribute method before calling the RenderBeginTag method. 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. 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

Remarks

Use 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.

For an instance of any given markup element, the HtmlTextWriter class maintains a list of attributes for that element. When the RenderBeginTag method is called, any attributes added by the AddAttribute method are rendered to the opening tag of the element. The list of attributes is then cleared from the HtmlTextWriter object.

The coding pattern for rendering markup elements is as follows:

  • Use the AddAttribute method to add any attributes to the element.

  • Use the RenderBeginTag method.

  • Use other methods as needed to render the content found between the element's opening and closing tags.

  • Use the RenderEndTag method.

See also

Applies to

AddAttribute(HtmlTextWriterAttribute, String)

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)

Parameters

key
HtmlTextWriterAttribute

An HtmlTextWriterAttribute that represents the markup attribute to add to the output stream.

value
String

A string containing the value to assign to the attribute.

Examples

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');")

Remarks

Use the AddAttribute overload of the AddAttribute(HtmlTextWriterAttribute, String) method to render a standard markup attribute.

For an instance of any given markup element, the HtmlTextWriter class maintains a list of attributes for that element. When the RenderBeginTag method is called, any attributes that are added by the AddAttribute method are rendered to the opening tag of the element. The list of attributes is then cleared from the HtmlTextWriter.

The coding pattern for rendering markup elements is as follows:

  • Use the AddAttribute method to add any attributes to the element.

  • Use the RenderBeginTag method.

  • Use other methods as needed to render the content found between the element's opening and closing tags.

  • Use the RenderEndTag method.

See also

Applies to

AddAttribute(String, String, Boolean)

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)

Parameters

name
String

A string containing the name of the attribute to add.

value
String

A string containing the value to assign to the attribute.

fEndode
Boolean

true to encode the attribute and its value; otherwise, false.

Examples

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 "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()

Remarks

Use 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.

For an instance of any given markup element, the HtmlTextWriter class maintains a list of attributes for that element. When the RenderBeginTag method is called, any attributes added by the AddAttribute method are rendered to the opening tag of the element. The list of attributes is then cleared from the HtmlTextWriter object.

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

If the attribute type is Style, the value won't be encoded even if fEndode is true. Ensure that the style value is CSS-compliant and doesn't contain malicious code.

The coding pattern for rendering markup elements is as follows:

  • Use the AddAttribute method to add any attributes to the element.

  • Use the RenderBeginTag method.

  • Use other methods as needed to render the content found between the element's opening and closing tags.

  • Use the RenderEndTag method.

See also

Applies to

AddAttribute(String, String, HtmlTextWriterAttribute)

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)

Parameters

name
String

A string containing the name of the attribute to add.

value
String

A string containing the value to assign to the attribute.

key
HtmlTextWriterAttribute

An HtmlTextWriterAttribute that represents the attribute.

Remarks

Use the AddAttribute overload of the AddAttribute(String, String, HtmlTextWriterAttribute) method only when inheriting from the HtmlTextWriter class. It enables you to create new name and key pairs for attributes.

See also

Applies to

AddAttribute(HtmlTextWriterAttribute, String, Boolean)

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)

Parameters

key
HtmlTextWriterAttribute

An HtmlTextWriterAttribute that represents the markup attribute to add to the output stream.

value
String

A string containing the value to assign to the attribute.

fEncode
Boolean

true to encode the attribute and its value; otherwise, false.

Examples

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 "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()

Remarks

Use the AddAttribute overload of the AddAttribute(HtmlTextWriterAttribute, String, Boolean) method to render a standard markup attribute, with optional encoding.

For an instance of any given markup element, the HtmlTextWriter class maintains a list of attributes for that element. When the RenderBeginTag method is called, any attributes added by the AddAttribute are rendered to the opening tag of the element. The list of attributes is then cleared from the HtmlTextWriter object.

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

If the attribute type is Style, the value won't be encoded even if fEncode is true. Ensure that the style value is CSS-compliant and doesn't contain malicious code.

The coding pattern for rendering markup elements is as follows:

  • Use the AddAttribute method to add any attributes to the element.

  • Use the RenderBeginTag method.

  • Use other methods as needed to render the content found between the element's opening and closing tags.

  • Use the RenderEndTag method.

See also

Applies to