HtmlTextWriter.AddStyleAttribute 方法

定义

将标记样式属性添加到 HtmlTextWriter 对象创建的元素的开始标记,随后调用 RenderBeginTag 方法。Adds a markup style attribute to the opening tag of the element that the HtmlTextWriter object creates with a subsequent call to the RenderBeginTag method.

重载

AddStyleAttribute(String, String)

对于通过对 RenderBeginTag 方法的后续调用创建的开始标记,向其中添加指定的标记样式属性和属性值。Adds the specified markup style attribute and the attribute value to the opening markup tag created by a subsequent call to the RenderBeginTag method.

AddStyleAttribute(HtmlTextWriterStyle, String)

对于通过对 HtmlTextWriterStyle 方法的后续调用而创建的开始标记,向其中添加与指定的 RenderBeginTag 值相关联的标记样式属性和属性值。Adds the markup style attribute associated with the specified HtmlTextWriterStyle value and the attribute value to the opening markup tag created by a subsequent call to the RenderBeginTag method.

AddStyleAttribute(String, String, HtmlTextWriterStyle)

对于通过对 HtmlTextWriterStyle 方法的后续调用而创建的开始标记,向其中添加指定的标记样式属和属性值以及 RenderBeginTag 枚举值。Adds the specified markup style attribute and the attribute value, along with an HtmlTextWriterStyle enumeration value, to the opening markup tag created by a subsequent call to the RenderBeginTag method.

AddStyleAttribute(String, String)

对于通过对 RenderBeginTag 方法的后续调用创建的开始标记,向其中添加指定的标记样式属性和属性值。Adds the specified markup style attribute and the attribute value to the opening markup tag created by a subsequent call to the RenderBeginTag method.

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

参数

name
String

包含要添加的样式属性的字符串。A string that contains the style attribute to add.

value
String

包含要分配给该属性的值的字符串。A string that contains the value to assign to the attribute.

示例

下面的代码示例演示RenderBeginTag如何使用AddStyleAttribute(String, String)方法的重载来呈现font-size <p>元素上的特性color和样式特性。The following code example shows how to use the RenderBeginTag overload of the AddStyleAttribute(String, String) method to render font-size and color style attributes on a <p> element. 此代码示例使用HtmlTextWriter类呈现控件的内容。This code example uses the HtmlTextWriter class to render the contents of the control.

// Add style attribute for 'p'(paragraph) element.
writer->AddStyleAttribute( "font-size", "12pt" );
writer->AddStyleAttribute( "color", "fuchsia" );
// Output the 'p' (paragraph) element with the style attributes.
writer->RenderBeginTag( "p" );
// Output the 'Message' property contents and the time on the server.
writer->Write( String::Concat( Message, "<br>",
   "The time on the server: ",
   System::DateTime::Now.ToLongTimeString() ) );

// Close the element.
writer->RenderEndTag();
// Add style attribute for 'p'(paragraph) element.
writer.AddStyleAttribute("font-size", "12pt");
writer.AddStyleAttribute("color", "fuchsia");
// Output the 'p' (paragraph) element with the style attributes.
writer.RenderBeginTag("p");
// Output the 'Message' property contents and the time on the server.
writer.Write(Message + "<br>" +
    "The time on the server: " +
    System.DateTime.Now.ToLongTimeString());

// Close the element.
writer.RenderEndTag();
'Add style attribute for 'p'(paragraph) element.
writer.AddStyleAttribute("font-size", "12pt")
writer.AddStyleAttribute("color", "fuchsia")

'Output the 'p' (paragraph) element with the style attributes.
writer.RenderBeginTag("p")

'Output the 'Message' property contents and the time on the server.
writer.Write((Message & "<br>" & "The time on the server: " & _
   System.DateTime.Now.ToLongTimeString()))

' Close the element.
writer.RenderEndTag()

注解

如果样式AddStyleAttribute(String, String)不是 AddStyleAttribute 枚举的成员,或者在运行时之前不是已知的,则使用HtmlTextWriterStyle方法的重载。Use the AddStyleAttribute overload of the AddStyleAttribute(String, String) method when the style is not a member of the HtmlTextWriterStyle enumeration or is not known until run time.

HtmlTextWriter类维护它所呈现的标记元素的样式列表。The HtmlTextWriter class maintains a list of styles for the markup elements it renders. 调用方法时, AddStyleAttribute方法添加的任何样式将呈现到元素的开始标记中。 RenderBeginTagWhen the RenderBeginTag method is called, any styles that are added by the AddStyleAttribute method are rendered to the opening tag of the element. 然后,将清除样式列表。The list of styles is then cleared.

呈现标记元素的编码模式如下所示:The coding pattern for rendering markup elements is as follows:

  • AddStyleAttribute使用方法可将任何样式特性添加到元素。Use the AddStyleAttribute method to add any style attributes to the element.

  • 使用 RenderBeginTag 方法。Use the RenderBeginTag method.

  • 根据需要使用其他方法来呈现在元素的开始标记和结束标记之间找到的内容。Use other methods as needed to render the content found between the element opening and closing tags.

  • 使用 RenderEndTag 方法。Use the RenderEndTag method.

另请参阅

AddStyleAttribute(HtmlTextWriterStyle, String)

对于通过对 HtmlTextWriterStyle 方法的后续调用而创建的开始标记,向其中添加与指定的 RenderBeginTag 值相关联的标记样式属性和属性值。Adds the markup style attribute associated with the specified HtmlTextWriterStyle value and the attribute value to the opening markup tag created by a subsequent call to the RenderBeginTag method.

public:
 virtual void AddStyleAttribute(System::Web::UI::HtmlTextWriterStyle key, System::String ^ value);
public virtual void AddStyleAttribute (System.Web.UI.HtmlTextWriterStyle key, string value);
abstract member AddStyleAttribute : System.Web.UI.HtmlTextWriterStyle * string -> unit
override this.AddStyleAttribute : System.Web.UI.HtmlTextWriterStyle * string -> unit
Public Overridable Sub AddStyleAttribute (key As HtmlTextWriterStyle, value As String)

参数

key
HtmlTextWriterStyle

表示将样式属性添加到输出流的 HtmlTextWriterStyleAn HtmlTextWriterStyle that represents the style attribute to add to the output stream.

value
String

包含要分配给该属性的值的字符串。A string that contains the value to assign to the attribute.

示例

下面的代码示例演示如何使用派生RenderBeginTag HtmlTextWriter自类的类中方法的部分重写。The following code example demonstrates how to use part of an override of the RenderBeginTag method in a class derived from the HtmlTextWriter class. 代码检查是否正在呈现<Label>某个元素。The code checks whether a <Label> element is being rendered. 如果是这样, IsStyleAttributeDefined则调用方法以检查Color是否已为<Label>元素定义了样式特性。If so, the IsStyleAttributeDefined method is called to check whether a Color style attribute has been defined for the <Label> element. 如果尚未定义AddStyleAttribute Color red属性,则代码将调用方法的此重载,将特性添加到 style 特性,然后将其值设置为。 ColorIf a Color attribute has not been defined, the code calls this overload of the AddStyleAttribute method to add the Color attribute to the style attribute, and then set its value to red.

// If the markup element being rendered is a Label,
// render the opening tag of a <Font> element before it.
if ( tagKey == HtmlTextWriterTag::Label )
{
   
   // Check whether a Color style attribute is
   // included on the Label. If not, use the
   // AddStyleAttribute and GetStyleName methods to add one
   // and set its value to red.
   if (  !IsStyleAttributeDefined( HtmlTextWriterStyle::Color ) )
   {
      AddStyleAttribute( GetStyleName( HtmlTextWriterStyle::Color ), "red" );
   }

// If the markup element being rendered is a Label,
// render the opening tag of a Font element before it.
if (tagKey == HtmlTextWriterTag.Label)
{
    // Check whether a Color style attribute is 
    // included on the Label. If not, use the
    // AddStyleAttribute and GetStyleName methods to add one
    // and set its value to red.
    if (!IsStyleAttributeDefined(HtmlTextWriterStyle.Color))
    {
        AddStyleAttribute(GetStyleName(HtmlTextWriterStyle.Color), "red");
    }
' If the markup element being rendered is a Label,
' render the opening tag of a Font element before it.
If tagKey = HtmlTextWriterTag.Label Then
    ' Check whether a Color style attribute is 
    ' included on the Label. If not, use the
    ' AddStyleAttribute and GetStyleName methods to add one
    ' and set its value to red.
    If Not IsStyleAttributeDefined(HtmlTextWriterStyle.Color) Then
        AddStyleAttribute(GetStyleName(HtmlTextWriterStyle.Color), "red")
    End If

注解

如果样式AddStyleAttribute(HtmlTextWriterStyle, String)是枚举的成员并且在运行时之前已知,请使用方法的重载。AddStyleAttribute HtmlTextWriterStyleUse the AddStyleAttribute overload of the AddStyleAttribute(HtmlTextWriterStyle, String) method when the style is a member of the HtmlTextWriterStyle enumeration and is known before run time.

HtmlTextWriter类维护它所呈现的标记元素的样式列表。The HtmlTextWriter class maintains a list of styles for the markup elements it renders. 调用方法时, AddStyleAttribute方法添加的任何样式将呈现到元素的开始标记中。 RenderBeginTagWhen the RenderBeginTag method is called, any styles added by the AddStyleAttribute method are rendered to the opening tag of the element. 然后,将清除样式列表。The list of styles is then cleared.

呈现标记元素的编码模式如下所示:The coding pattern for rendering markup elements is as follows:

  • AddStyleAttribute使用方法可将任何样式特性添加到元素。Use the AddStyleAttribute method to add any style attributes to the element.

  • 使用 RenderBeginTag 方法。Use the RenderBeginTag method.

  • 根据需要使用其他方法来呈现在元素的开始标记和结束标记之间找到的内容。Use other methods as needed to render the content found between the element opening and closing tags.

  • 使用 RenderEndTag 方法。Use the RenderEndTag method.

另请参阅

AddStyleAttribute(String, String, HtmlTextWriterStyle)

对于通过对 HtmlTextWriterStyle 方法的后续调用而创建的开始标记,向其中添加指定的标记样式属和属性值以及 RenderBeginTag 枚举值。Adds the specified markup style attribute and the attribute value, along with an HtmlTextWriterStyle enumeration value, to the opening markup tag created by a subsequent call to the RenderBeginTag method.

protected:
 virtual void AddStyleAttribute(System::String ^ name, System::String ^ value, System::Web::UI::HtmlTextWriterStyle key);
protected virtual void AddStyleAttribute (string name, string value, System.Web.UI.HtmlTextWriterStyle key);
abstract member AddStyleAttribute : string * string * System.Web.UI.HtmlTextWriterStyle -> unit
override this.AddStyleAttribute : string * string * System.Web.UI.HtmlTextWriterStyle -> unit
Protected Overridable Sub AddStyleAttribute (name As String, value As String, key As HtmlTextWriterStyle)

参数

name
String

包含要添加的样式属性的字符串。A string that contains the style attribute to be added.

value
String

包含要分配给该属性的值的字符串。A string that contains the value to assign to the attribute.

key
HtmlTextWriterStyle

表示要添加的样式属性的 HtmlTextWriterStyleAn HtmlTextWriterStyle that represents the style attribute to add.

注解

仅当从AddStyleAttribute(String, String, HtmlTextWriterStyle) AddStyleAttribute 类继承时,才使用HtmlTextWriter方法的重载。Use the AddStyleAttribute overload of the AddStyleAttribute(String, String, HtmlTextWriterStyle) method only when inheriting from the HtmlTextWriter class. 它使您能够为属性name创建value新的HtmlTextWriterStyle和对。It enables you to create new name and value pairs for HtmlTextWriterStyle attributes.

另请参阅

适用于