CustomStyle Class
When dynamically generating repeated content for a Page, I have had situations where I need to dynamically determine the style as well. Because setting the style attribute for every one of the repeated items can greatly increase the size of the download for the user, I wanted to learn how to dynamically add CSS classes to my stylesheet more efficiently. After finally learning this, I created this class to simplify the process.

Here are some very basic examples of how to use the CustomStyle class. They give the style rule and the statement needed to create the style rule in a Web Content Form or Web Form:

To create the style rule:
Use one of the following statements:
Me.Page.Header.StyleSheet.CreateStyleRule(New CustomStyle("margin:0px;padding:0px;"),Nothing,".zeromarginpadding")
Me.Header.StyleSheet.CreateStyleRule(New CustomStyle("margin:0px;padding:0px;"),Nothing,".zeromarginpadding")

To create the style rule:
Use one of the following statements:
Me.Page.Header.StyleSheet.CreateStyleRule(New CustomStyle("border-width:0px;width:100%;"),Nothing,"div")
Me.Header.StyleSheet.CreateStyleRule(New CustomStyle("border-width:0px;width:100%;"),Nothing,"div")
Properties & Methods
CustomStyleAll methods and properties of the CustomStyle class other than the two constructors listed here are inherited from or are overrides of the ones defined in System.Web.UI.WebControls.Style. See the documentation for more information on these methods and properties. CustomStyle(custom As CssStyleCollection)Creates a new CustomStyle from a CssStyleCollection
  • custom - The CssStyleCollection to create a CustomStyle from
CustomStyle(cssvalue as String)Creates a new CustomStyle from a String of CSS properties and values
  • cssvalue - A string containing CSS properties and values
Source Code