HtmlTextWriter.EncodeAttributeValue Method

Definition

Encodes the value of the specified markup attribute based on the requirements of the HttpRequest object of the current context.

Overloads

EncodeAttributeValue(String, Boolean)

Encodes the value of the specified markup attribute based on the requirements of the HttpRequest object of the current context.

EncodeAttributeValue(HtmlTextWriterAttribute, String)

Encodes the value of the specified markup attribute based on the requirements of the HttpRequest object of the current context.

EncodeAttributeValue(String, Boolean)

Encodes the value of the specified markup attribute based on the requirements of the HttpRequest object of the current context.

C#
protected string EncodeAttributeValue(string value, bool fEncode);

Parameters

value
String

A string containing the attribute value to encode.

fEncode
Boolean

true to encode the attribute value; otherwise, false.

Returns

A string containing the encoded attribute value, null if value is empty, or the unencoded attribute value if fEncode is false.

Examples

The following code example shows how to call the EncodeAttributeValue method as a parameter in an AddAttribute method call, and then encodes a color:blue style attribute value.

C#
// If the <label> element is rendered and a style
// attribute is not defined, add a style attribute 
// and set its value to blue.
if (TagKey == HtmlTextWriterTag.Label)
{
    if (!IsAttributeDefined(HtmlTextWriterAttribute.Style))
    {
        AddAttribute("style", EncodeAttributeValue("color:blue", true));
        Write(NewLine);
        Indent = 3;
        OutputTabs();
    }
}

Remarks

Use the EncodeAttributeValue overload of the EncodeAttributeValue(HtmlTextWriterAttribute, String) method if the attribute is not an HtmlTextWriterAttribute enumeration value or is not known until run time.

The EncodeAttributeValue method removes double quotation marks ("), ampersands (&), and less than signs (<) so that invalid tags are not generated, regardless of the input. The actual encoding is performed by the HtmlAttributeEncode method.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

EncodeAttributeValue(HtmlTextWriterAttribute, String)

Encodes the value of the specified markup attribute based on the requirements of the HttpRequest object of the current context.

C#
protected virtual string EncodeAttributeValue(System.Web.UI.HtmlTextWriterAttribute attrKey, string value);

Parameters

attrKey
HtmlTextWriterAttribute

An HtmlTextWriterAttribute representing the markup attribute.

value
String

A string containing the attribute value to encode.

Returns

A string containing the encoded attribute value.

Remarks

The EncodeAttributeValue method removes double quotation marks ("), ampersands (&), and less than signs (<) so that invalid tags are not generated, regardless of the input. The actual encoding is performed by the HtmlAttributeEncode method.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1