ListItemCollection.Add Method

Definition

Appends a ListItem to the end of the collection.

Overloads

Add(String)

Appends a ListItem to the end of the collection that represents the specified string.

Add(ListItem)

Appends the specified ListItem to the end of the collection.

Add(String)

Appends a ListItem to the end of the collection that represents the specified string.

C#
public void Add(string item);

Parameters

item
String

A String that represents the item to add to the end of the collection.

Examples

The following code example demonstrates creating ListItemCollection objects, adding items to the collections, and removing items from the collections. To view this code in a fully working example, see the WebControl class topic.

C#
// Create a new ListItemCollection.
ListItemCollection listBoxData = new ListItemCollection();
// Add items to the collection.
listBoxData.Add(new ListItem("apples"));
listBoxData.Add(new ListItem("bananas"));
listBoxData.Add(new ListItem("cherries"));
listBoxData.Add("grapes");
listBoxData.Add("mangos");
listBoxData.Add("oranges");
// Set the ListItemCollection as the data source for ListBox1.
ListBox1.DataSource = listBoxData;
ListBox1.DataBind();

Remarks

Use the Add method to append a ListItem to the end of the collection. This implementation of the method creates a ListItem to represent the text specified by the item parameter. This ListItem is then appended to the collection.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Add(ListItem)

Appends the specified ListItem to the end of the collection.

C#
public void Add(System.Web.UI.WebControls.ListItem item);

Parameters

item
ListItem

The ListItem to append to the collection.

Remarks

Use the Add method to append a ListItem to the end of the collection. This implementation of the method takes the ListItem specified by the item parameter and appends it to the collection.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1