HtmlTextWriter.EncodeAttributeValue 方法

定義

根據目前內容中 HttpRequest 物件的需求來編碼指定標記屬性的值。

多載

EncodeAttributeValue(String, Boolean)

根據目前內容中 HttpRequest 物件的需求來編碼指定標記屬性的值。

EncodeAttributeValue(HtmlTextWriterAttribute, String)

根據目前內容中 HttpRequest 物件的需求來編碼指定標記屬性的值。

EncodeAttributeValue(String, Boolean)

根據目前內容中 HttpRequest 物件的需求來編碼指定標記屬性的值。

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

包含要編碼的屬性值的字串。

fEncode
Boolean

true 表示要編碼屬性值;否則為 false

傳回

包含已編碼屬性值的字串、如果 value 是空的則為 null,或者如果 fEncodefalse,則為未編碼的屬性值。

範例

下列程式代碼範例示範如何在方法呼叫中AddAttribute呼叫 EncodeAttributeValue 方法做為參數,然後編碼color:blue樣式屬性值。

// 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列舉值,或在運行時間之前不知道,請使用 方法的多載EncodeAttributeValue(HtmlTextWriterAttribute, String)

方法 EncodeAttributeValue 會移除雙引號 (“) 、ampersands (&) ,以及小於符號 () < ,因此不論輸入為何,都不會產生無效的標記。 實際的編碼方式是由 HtmlAttributeEncode 方法執行。

另請參閱

適用於

EncodeAttributeValue(HtmlTextWriterAttribute, String)

根據目前內容中 HttpRequest 物件的需求來編碼指定標記屬性的值。

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

HtmlTextWriterAttribute,表示該標記屬性。

value
String

包含要編碼的屬性值的字串。

傳回

字串,包含已編碼的屬性值。

備註

方法 EncodeAttributeValue 會移除雙引號 (“) 、ampersands (&) ,以及小於符號 () < ,因此不論輸入為何,都不會產生無效的標記。 實際的編碼方式是由 HtmlAttributeEncode 方法執行。

另請參閱

適用於