This documentation is archived and is not being maintained.

ListItemCollection.Add Method

Appends a ListItem to the end of the collection.

Overload List

Appends the specified ListItem to the end of the collection.

[Visual Basic] Overloads Public Sub Add(ListItem)
[C#] public void Add(ListItem);
[C++] public: void Add(ListItem*);
[JScript] public function Add(ListItem);

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

[Visual Basic] Overloads Public Sub Add(String)
[C#] public void Add(string);
[C++] public: void Add(String*);
[JScript] public function Add(String);

Example

[Visual Basic, C#] Note   This example shows how to use one of the overloaded versions of Add. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
<!-- This example demonstrates creating ListItemCollection objects, 
adding items to the collections, and removing items from the collections. -->
    . . . 
<!-- To view this code snippet in a fully-working example, see the 
WebControl Class topic. -->

    . . . 
                    ' Create a new ListItemCollection.
                    Dim listBoxData As 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()

[C#] 
<!-- This example demonstrates creating ListItemCollection objects, 
adding items to the collections, and removing items from the collections. -->
    . . . 
<!-- To view this code snippet in a fully-working example, see the 
WebControl Class topic. -->

    . . . 
                    // 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();

[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.

See Also

ListItemCollection Class | ListItemCollection Members | System.Web.UI.WebControls Namespace

Show: