HtmlTextWriter.AddStyleAttribute Method (HtmlTextWriterStyle, String)
Assembly: System.Web (in System.Web.dll)
Public Overridable Sub AddStyleAttribute ( key As HtmlTextWriterStyle, value As String )
Use the HtmlTextWriterStyle enumeration and is known before run time.overload of the method when the style is 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 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 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 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
Available since 1.1