Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

HtmlTextWriter.AddStyleAttribute Method (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.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)
public virtual void AddStyleAttribute(
	string name,
	string value


Type: System.String

A string that contains the style attribute to add.

Type: System.String

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

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.

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.
// Output the 'Message' property contents and the time on the server.
writer.Write(Message + "<br>" +
    "The time on the server: " +

// Close the element.

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft. All rights reserved.