HtmlTextWriter.WriteAttribute 方法

定义

将标记属性及其值写入到输出流。Writes a markup attribute and its value to the output stream.

重载

WriteAttribute(String, String, Boolean)

将指定标记特性和值写入输出流,并且(如果指定)写入经过编码的值。Writes the specified markup attribute and value to the output stream, and, if specified, writes the value encoded.

WriteAttribute(String, String)

将指定的标记属性和值写入到输出流。Writes the specified markup attribute and value to the output stream.

WriteAttribute(String, String, Boolean)

将指定标记特性和值写入输出流,并且(如果指定)写入经过编码的值。Writes the specified markup attribute and value to the output stream, and, if specified, writes the value encoded.

public:
 virtual void WriteAttribute(System::String ^ name, System::String ^ value, bool fEncode);
public virtual void WriteAttribute (string name, string value, bool fEncode);
abstract member WriteAttribute : string * string * bool -> unit
override this.WriteAttribute : string * string * bool -> unit
Public Overridable Sub WriteAttribute (name As String, value As String, fEncode As Boolean)

参数

name
String

要写入输出流的标记特性。The markup attribute to write to the output stream.

value
String

要分配给特性的值。The value assigned to the attribute.

fEncode
Boolean

若要对特性及其分配值进行编码,则为 true;否则为 falsetrue to encode the attribute and its assigned value; otherwise, false.

示例

下面的代码示例演示如何呈现<img>一个元素,在该元素中WriteAttribute调用方法的两个重载。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:

此代码示例生成以下标记:This code example generates the following markup:

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

</img>

// Create a manually rendered tag.
writer->WriteBeginTag( "img" );
writer->WriteAttribute( "alt", "AtlValue" );
writer->WriteAttribute( "myattribute", "No &quot;encoding &quot; required", false );
writer->Write( HtmlTextWriter::TagRightChar );
writer->WriteEndTag( "img" );
// Create a manually rendered tag.
writer.WriteBeginTag("img");
writer.WriteAttribute("alt", "AtlValue");
writer.WriteAttribute("myattribute", "No &quot;encoding &quot; required", false);
writer.Write(HtmlTextWriter.TagRightChar);
writer.WriteEndTag("img");
' Create a manually rendered tag.
writer.WriteBeginTag("img")
writer.WriteAttribute("alt", "AtlValue")
writer.WriteAttribute("myattribute", "No &quot;encoding &quot; required", False)
writer.Write(HtmlTextWriter.TagRightChar)

注解

WriteAttribute使用方法可在不进行编码的情况下编写标记特性及其值。Use the WriteAttribute method to write markup attributes and their values with or without encoding. WriteAttribute 方法HtmlAttributeEncode使用方法来执行编码。The WriteAttribute method uses the HtmlAttributeEncode method to do the encoding.

WriteAttribute方法写入用双引号(")引起来的特性值。The WriteAttribute method writes the attribute value enclosed in double quotation marks ("). 如果value WriteAttributenull,则此方法只写入特性名称。If value is null, the WriteAttribute method writes only the attribute name.

继承者说明

在派生类中,可以重写WriteAttribute(String, String, Boolean)方法,以定义用于编写属性及其值的自定义规则。In derived classes, you can override the WriteAttribute(String, String, Boolean) method to define custom rules for writing attributes and their values. 例如,你可以编写代码,以允许WriteAttribute(String, String, Boolean)方法编写具有空值的属性。For example, you could write code that allowed the WriteAttribute(String, String, Boolean) method to write an attribute with an empty value.

另请参阅

WriteAttribute(String, String)

将指定的标记属性和值写入到输出流。Writes the specified markup attribute and value to the output stream.

public:
 virtual void WriteAttribute(System::String ^ name, System::String ^ value);
public virtual void WriteAttribute (string name, string value);
abstract member WriteAttribute : string * string -> unit
override this.WriteAttribute : string * string -> unit
Public Overridable Sub WriteAttribute (name As String, value As String)

参数

name
String

要写入到输出流的属性。The attribute to write to the output stream.

value
String

要分配给特性的值。The value assigned to the attribute.

示例

下面的代码示例演示如何呈现<img>一个元素,在该元素中WriteAttribute调用方法的两个重载。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:

此代码示例生成以下标记:This code example generates the following markup:

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

</img>

// Create a manually rendered tag.
writer->WriteBeginTag( "img" );
writer->WriteAttribute( "alt", "AtlValue" );
writer->WriteAttribute( "myattribute", "No &quot;encoding &quot; required", false );
writer->Write( HtmlTextWriter::TagRightChar );
writer->WriteEndTag( "img" );
// Create a manually rendered tag.
writer.WriteBeginTag("img");
writer.WriteAttribute("alt", "AtlValue");
writer.WriteAttribute("myattribute", "No &quot;encoding &quot; required", false);
writer.Write(HtmlTextWriter.TagRightChar);
writer.WriteEndTag("img");
' Create a manually rendered tag.
writer.WriteBeginTag("img")
writer.WriteAttribute("alt", "AtlValue")
writer.WriteAttribute("myattribute", "No &quot;encoding &quot; required", False)
writer.Write(HtmlTextWriter.TagRightChar)

注解

WriteAttribute使用方法来编写标记特性及其值,不进行编码。Use the WriteAttribute method to write markup attributes and their values with no encoding. WriteAttribute方法写入用双引号(")引起来的特性值。The WriteAttribute method writes the attribute value enclosed in double quotation marks ("). 如果value WriteAttributenull,则此方法只写入特性名称。If value is null, the WriteAttribute method writes only the attribute name.

继承者说明

在派生类中,可以重写WriteAttribute(String, String)方法,以定义用于编写属性及其值的自定义规则。In derived classes, you can override the WriteAttribute(String, String) method to define custom rules for writing attributes and their values. 例如,你可以编写代码,以允许WriteAttribute(String, String)方法编写具有空值的属性。For example, you could write code that allowed the WriteAttribute(String, String) method to write an attribute with an empty value.

另请参阅

适用于