HtmlTextWriter.EncodeAttributeValue Method (String, Boolean)

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

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

protected:
String^ EncodeAttributeValue (
	String^ value, 
	bool fEncode
)
protected String EncodeAttributeValue (
	String value, 
	boolean fEncode
)
protected function EncodeAttributeValue (
	value : String, 
	fEncode : boolean
) : String
Not applicable.

Parameters

value

A string containing the attribute value to encode.

fEncode

true to encode the attribute value; otherwise, false.

Return Value

A string containing the encoded attribute value, a null reference (Nothing in Visual Basic) if value is empty, or the unencoded attribute value if fEncode is false.

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.

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.

// 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();
   }
}

// If the Label tag is being rendered and a style
// attribute is not defined, add a style attribute 
// and set its value to blue.
if (get_TagKey().Equals(HtmlTextWriterTag.Label)) {
    if (!(IsAttributeDefined(HtmlTextWriterAttribute.Style))) {
        AddAttribute("style", EncodeAttributeValue("color:blue",
            true));
        Write(get_NewLine());
        set_Indent(3);
        OutputTabs();
    }
}

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show: