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.
Assembly: System.Web (in System.Web.dll)
Use the HtmlTextWriterStyle enumeration or is not known until run time.overload of the method when the style is not a member of the
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:
The following code example shows how to use the RenderBeginTag overload of the 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(Message + "<br>" + "The time on the server: " + System.DateTime.Now.ToLongTimeString()); // Close the element. writer.RenderEndTag();
Available since 1.1