System.Web.UI.WebControls.ListItem Class

Represents a data item in a data-bound list control. This class cannot be inherited.

See Also: ListItem Members

Syntax

[System.ComponentModel.TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
[System.Web.UI.ControlBuilder(typeof(System.Web.UI.WebControls.ListItemControlBuilder))]
public sealed class ListItem : System.Web.UI.IAttributeAccessor, System.Web.UI.IParserAccessor, System.Web.UI.IStateManager

Remarks

A System.Web.UI.WebControls.ListItem control represents an individual data item within a data-bound list control, such as a System.Web.UI.WebControls.ListBox or a System.Web.UI.WebControls.RadioButtonList control.

There are several ways to specify the text displayed for an item in the list control. The most common method is by placing text in the inner HTML content. The inner HTML content is the text between the opening and closing tags of the System.Web.UI.WebControls.ListItem control. You can also use the ListItem.Text property to specify the text displayed in the list control for the item.

The ListItem.Value property allows you to associate a value with the item in the list control, in addition to the text displayed in the control. For example, you can display text for an item in the list control, such as "Item 1", and use the ListItem.Value property to specify a value for that item, such as "$1.99".

You can have any combination of the inner HTML content, ListItem.Text, or ListItem.Value properties set. The resulting HTML output for the System.Web.UI.WebControls.ListItem control depends on the combination of these three properties that are set. For example, if all three properties are set as follows:

Example

<asp:ListItem Value="Value 1" Text="Item 1">Inner 1</asp:ListItem>

The inner HTML content is used for rendered inner HTML content and the ListItem.Value property is used for the Value attribute. The resulting HTML rendering output is:

Example

<option value="Value 1">Inner 1</option>

The following table lists the combination of set properties and the corresponding property used for the rendered inner HTML content and Value attribute. The three columns on the left list the combination of set properties. The two columns on the right list which property value is used for the corresponding attribute.

Set

Set

Set

Inner HTML content

Value property

Set

Set

Not set

Inner HTML content

Inner HTML content

Set

Not set

Set

Inner HTML content

Value property

Set

Not set

Not set

Inner HTML content

Inner HTML text

Not set

Set

Set

Text property

Value property

Not set

Set

Not set

Text property

Text property

Not set

Not set

Set

Value property

Value property

Not set

Not set

Not set

Not set

Not set

Note:

Because the ListItem.Text and ListItem.Value properties each have a default value of an empty string, it is possible to have empty list items in the list control.

When a list control is displayed, any System.Web.UI.WebControls.ListItem control with its ListItem.Selected property set to true appears highlighted in the control.

The System.Web.UI.WebControls.ListItem control provides the ListItem.Enabled property to allow you to specify whether a System.Web.UI.WebControls.ListItem control is enabled or disabled. A System.Web.UI.WebControls.ListItem control that is disabled is dimmed to indicate that it cannot be selected. Use this property to disable a System.Web.UI.WebControls.ListItem control in either a System.Web.UI.WebControls.RadioButtonList control or a System.Web.UI.WebControls.CheckBoxList control.

Note:

You cannot use this property to disable a System.Web.UI.WebControls.ListItem control in a System.Web.UI.WebControls.DropDownList control or System.Web.UI.WebControls.ListBox control.

For a list of initial property values for an instance of System.Web.UI.WebControls.ListItem, see the ListItem.#ctor constructor.

Note:

This control can be used to display user input, which might include malicious client script. Check any information that is sent from a client for executable script, SQL statements, or other code before displaying it in your application. You can use validation controls to verify user input before displaying the input text in a control. ASP.NET provides an input request validation feature to block script and HTML in user input. For more information, see Securing Standard ControlsHow to: Protect Against Script Exploits in a Web Application by Applying HTML Encoding to Strings, and Introduction to Validating User Input in ASP.NET Web Pages.

Requirements

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in System.Web.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0