Export (0) Print
Expand All

CssStyleCollection Class

Contains the HTML cascading-style sheets (CSS) inline style attributes for a specified HTML server control. This class cannot be inherited.

For a list of all members of this type, see CssStyleCollection Members.

System.Object
   System.Web.UI.CssStyleCollection

[Visual Basic]
NotInheritable Public Class CssStyleCollection
[C#]
public sealed class CssStyleCollection
[C++]
public __gc __sealed class CssStyleCollection
[JScript]
public class CssStyleCollection

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

Any style declared for a particular HTML server control is added to the collection when the containing Web Forms page is parsed. It automatically parses and exposes CSS properties through a dictionary pattern API. You can manipulate any CSS property on a server control through the Style property. Simply use the CSS property's key and value in the indexed collection.

Example

[Visual Basic, C#] The following example iterates through the CssStyleCollection for an HtmlInputText server control. Using the Keys property, the example determines which style attributes have been declared on the server control and displays those attributes to the containing Web Forms page.

[Visual Basic] 
<html>
 <script language="vb" runat="server">
   Sub Page_Load(Src As Object, e As EventArgs)
      Message.InnerHtml += "<h5>The Text Box's CssStyleCollection contains:</h5>"
   
      Dim keys As IEnumerator = MyText.Style.Keys.GetEnumerator()
   
      While keys.MoveNext()
      
         Dim key As [String] = CType(keys.Current, [String])
         Message.InnerHtml += key + "=" + MyText.Style(key) + "<br>"
      End While
   End Sub 'Page_Load
 </script>

 <body>
    <span id="Message" MaintainState="false" runat="server" />
    <p>
    <b>Enter some text:</b> <br>
    <input id="MyText" type="text" value="Type a value here." 
        style="font: 14pt verdana;width:300;" runat="server"/>
 
  </body>
</html>

[C#] 
<html>
 <script language="C#" runat="server">
    void Page_Load(Object Src, EventArgs e) {
       Message.InnerHtml += "<h5>The Text Box's CssStyleCollection contains:</h5>";
       
       IEnumerator keys = MyText.Style.Keys.GetEnumerator();
 
       while (keys.MoveNext()) {
 
           String key = (String)keys.Current;
           Message.InnerHtml += key + "=" + MyText.Style[key] + "<br>";
       }
    }
 </script>
 <body>
    <span id="Message" MaintainState="false" runat="server" />
    <p>
    <b>Enter some text:</b> <br>
    <input id="MyText" type="text" value="Type a value here." 
        style="font: 14pt verdana;width:300;" runat="server"/>
 
  </body>
</html>

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.UI

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

Assembly: System.Web (in System.Web.dll)

See Also

CssStyleCollection Members | System.Web.UI Namespace | Style

Show:
© 2014 Microsoft