HtmlTextWriter.EncodeAttributeValue 方法

定义

根据当前上下文的 HttpRequest 对象的要求,对指定标记特性的值进行编码。Encodes the value of the specified markup attribute based on the requirements of the HttpRequest object of the current context.

重载

EncodeAttributeValue(String, Boolean)

根据当前上下文的 HttpRequest 对象的要求,对指定标记特性的值进行编码。Encodes the value of the specified markup attribute based on the requirements of the HttpRequest object of the current context.

EncodeAttributeValue(HtmlTextWriterAttribute, String)

根据当前上下文的 HttpRequest 对象的要求,对指定标记特性的值进行编码。Encodes the value of the specified markup attribute based on the requirements of the HttpRequest object of the current context.

EncodeAttributeValue(String, Boolean)

根据当前上下文的 HttpRequest 对象的要求,对指定标记特性的值进行编码。Encodes the value of the specified markup attribute based on the requirements of the HttpRequest object of the current context.

protected:
 System::String ^ EncodeAttributeValue(System::String ^ value, bool fEncode);
protected string EncodeAttributeValue (string value, bool fEncode);
member this.EncodeAttributeValue : string * bool -> string
Protected Function EncodeAttributeValue (value As String, fEncode As Boolean) As String

参数

value
String

包含要编码的特性值的字符串。A string containing the attribute value to encode.

fEncode
Boolean

若要对特性值进行编码,则为 true;否则为 falsetrue to encode the attribute value; otherwise, false.

返回

包含已编码特性值的字符串;如果 value 为空,则为 null;如果 fEncodefalse,则为包含未编码特性值的字符串。A string containing the encoded attribute value, null if value is empty, or the unencoded attribute value if fEncode is false.

示例

下面的代码示例演示如何将EncodeAttributeValue方法作为AddAttribute方法调用中的参数调用color:blue ,然后对样式特性值进行编码。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> 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> element is rendered and a style
' attribute is not defined, add a style attribute 
' and set its value to blue.
If TagKey = HtmlTextWriterTag.Label Then
    If Not IsAttributeDefined(HtmlTextWriterAttribute.Style) Then
        AddAttribute("style", EncodeAttributeValue("color:blue", True))
        Write(NewLine)
        Indent = 3
        OutputTabs()
    End If
End If

注解

如果该特性不是EncodeAttributeValue(HtmlTextWriterAttribute, String) EncodeAttributeValue 枚举值,或者在运行时之前不是已知的,请使用HtmlTextWriterAttribute方法的重载。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.

EncodeAttributeValue方法可删除双引号(")、与号(&)和小于符号(<),以便不会生成无效标记,而不管输入如何。The EncodeAttributeValue method removes double quotation marks ("), ampersands (&), and less than signs (<) so that invalid tags are not generated, regardless of the input. 实际编码由HtmlAttributeEncode方法执行。The actual encoding is performed by the HtmlAttributeEncode method.

另请参阅

EncodeAttributeValue(HtmlTextWriterAttribute, String)

根据当前上下文的 HttpRequest 对象的要求,对指定标记特性的值进行编码。Encodes the value of the specified markup attribute based on the requirements of the HttpRequest object of the current context.

protected:
 virtual System::String ^ EncodeAttributeValue(System::Web::UI::HtmlTextWriterAttribute attrKey, System::String ^ value);
protected virtual string EncodeAttributeValue (System.Web.UI.HtmlTextWriterAttribute attrKey, string value);
abstract member EncodeAttributeValue : System.Web.UI.HtmlTextWriterAttribute * string -> string
override this.EncodeAttributeValue : System.Web.UI.HtmlTextWriterAttribute * string -> string
Protected Overridable Function EncodeAttributeValue (attrKey As HtmlTextWriterAttribute, value As String) As String

参数

attrKey
HtmlTextWriterAttribute

表示标记属性的 HtmlTextWriterAttributeAn HtmlTextWriterAttribute representing the markup attribute.

value
String

包含要编码的特性值的字符串。A string containing the attribute value to encode.

返回

包含已编码的属性值的字符串。A string containing the encoded attribute value.

注解

EncodeAttributeValue方法可删除双引号(")、与号(&)和小于符号(<),以便不会生成无效标记,而不管输入如何。The EncodeAttributeValue method removes double quotation marks ("), ampersands (&), and less than signs (<) so that invalid tags are not generated, regardless of the input. 实际编码由HtmlAttributeEncode方法执行。The actual encoding is performed by the HtmlAttributeEncode method.

另请参阅

适用于