Redigeeri

Jagamisviis:


HtmlTextWriter.AddStyleAttribute Method

Definition

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.

Overloads

AddStyleAttribute(String, String)

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)

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)

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)

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)

Parameters

name
String

A string that contains the style attribute to add.

value
String

A string that contains the value to assign to the attribute.

Examples

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. 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()

Remarks

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.

The HtmlTextWriter class maintains a list of styles for the markup elements it renders. When 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:

  • Use the AddStyleAttribute method to add any style attributes to the element.

  • Use the RenderBeginTag method.

  • Use other methods as needed to render the content found between the element opening and closing tags.

  • Use the RenderEndTag method.

See also

Applies to

AddStyleAttribute(HtmlTextWriterStyle, String)

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)

Parameters

key
HtmlTextWriterStyle

An 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.

Examples

The following code example demonstrates how to use part of an override of the RenderBeginTag method in a class derived from the HtmlTextWriter class. The code checks whether a <Label> element is being rendered. If so, the IsStyleAttributeDefined method is called to check whether a Color style attribute has been defined for the <Label> element. If 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

Remarks

Use 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.

The HtmlTextWriter class maintains a list of styles for the markup elements it renders. When 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:

  • Use the AddStyleAttribute method to add any style attributes to the element.

  • Use the RenderBeginTag method.

  • Use other methods as needed to render the content found between the element opening and closing tags.

  • Use the RenderEndTag method.

See also

Applies to

AddStyleAttribute(String, String, HtmlTextWriterStyle)

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)

Parameters

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

An HtmlTextWriterStyle that represents the style attribute to add.

Remarks

Use the AddStyleAttribute overload of the AddStyleAttribute(String, String, HtmlTextWriterStyle) method only when inheriting from the HtmlTextWriter class. It enables you to create new name and value pairs for HtmlTextWriterStyle attributes.

See also

Applies to