This documentation is archived and is not being maintained.

CssStyleCollection.Item Property (HtmlTextWriterStyle)

Gets or sets the specified HtmlTextWriterStyle value for the HTML server control.

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

public string this[
	HtmlTextWriterStyle key
] { get; set; }

Property Value

Type: System.String
The value key; otherwise, null, if key is not in the server control's collection.

Style items that are added programmatically to a CssStyleCollection collection for a control with the Item property or the Add method that take an HtmlTextWriterStyle enumeration value as an input are included in the count of style items that are returned with the Count property and are included with the string literal that is returned with the Value property. These attributes are strongly typed. If the cascading style sheet attribute that you want to add to a server control exists in the HtmlTextWriterStyle enumeration, use these strongly typed methods. If the attribute you want to add does not exist in the HtmlTextWriterStyle enumeration, use the Item property or the Add method.

Trying to access an HtmlTextWriterStyle enumeration value in the CssStyleCollection of a control that has not been added or to access a CSS attribute that has been added with the Item property or the Add method, returns null using the Item property.

The following code example uses the Item property to return the value of a style that is set on a HtmlInputText control.


<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  protected void Page_Load(object sender, EventArgs e)
  {
    MyText.Style.Add(HtmlTextWriterStyle.Width, "200");
    FirstMessage.Text = "The text box font color is: " + 
      MyText.Style["color"] + "<br />" +
      "The text box width is: " + 
      MyText.Style[HtmlTextWriterStyle.Width];
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>CssCollection This Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <input id="MyText"
             type="text" 
             value="Type a value here."
             style="font: 14pt Verdana; color: blue;"
             runat="server"/>
      <br />
      <asp:Label id="FirstMessage"
                 runat="server"/>
    </div>
    </form>
</body>
</html>


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Show: