AttributeCollection.CssStyle Property


Gets a collection of styles for the ASP.NET server control to which the current AttributeCollection object belongs.

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

public CssStyleCollection CssStyle { get; }

Property Value

Type: System.Web.UI.CssStyleCollection

A collection that contains the styles for the current server control.

Any style declared for a particular HTML server control is added to the CssStyleCollection object when the containing ASP.NET Web page is parsed. You can use this property to add, remove, and iterate through the styles declared for the server control.

The following example shows how to add a CSS style value programmatically to a server control's AttributeCollection object. When a button is clicked, the background color of a TextBox server control named TextBox1 is set to the selected item in a DropDownList server control named DropDownList1.

private void Button2_Click(object sender, System.EventArgs e)

   string myColor;
   AttributeCollection myAttributes = TextBox1.Attributes;
   myColor = DropDownList1.Items[DropDownList1.SelectedIndex].Text;
   // Add the attribute "background-color" in to the CssStyle.


.NET Framework
Available since 1.1
Return to top