HtmlTextWriter.WriteAttribute Method (String, String, Boolean)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Writes the specified markup attribute and value to the output stream, and, if specified, writes the value encoded.

Namespace:   System.Web.UI
Assembly:  System.Web (in System.Web.dll)

public virtual void WriteAttribute(
	string name,
	string value,
	bool fEncode


Type: System.String

The markup attribute to write to the output stream.

Type: System.String

The value assigned to the attribute.

Type: System.Boolean

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

Use the WriteAttribute method to write markup attributes and their values with or without encoding. The WriteAttribute method uses the HtmlAttributeEncode method to do the encoding.

The WriteAttribute method writes the attribute value enclosed in double quotation marks ("). If value is null, the WriteAttribute method writes only the attribute name.

Notes to Inheritors:

In derived classes, you can override the WriteAttribute method to define custom rules for writing attributes and their values. For example, you could write code that allowed the WriteAttribute method to write an attribute with an empty value.

The following code example demonstrates rendering an <img> element in which both overloads of the WriteAttribute method are called. The code example uses the following process:

  • Calls the WriteBeginTag method, which renders the opening characters of the element.

  • Calls the WriteAttribute(String, String) overload, which writes an alt attribute and its value to the <img> element.

  • Calls the WriteAttribute(String, String, Boolean) overload to render a custom myattribute attribute, with a value of No "encoding" required, and then sets fEncode to false.

  • Closes the opening tag of the element, and then calls the WriteEndTag method to close the <img> element.

This code example generates the following markup:

<img alt="AtlValue" myattribute="No &quot;encoding&quot; required">


// Create a manually rendered tag.
writer.WriteAttribute("alt", "AtlValue");
writer.WriteAttribute("myattribute", "No &quot;encoding &quot; required", false);

.NET Framework
Available since 1.1
Return to top