JavaScript Editor jscript editor     Web designer 

Main Page

The properties of some Web server controls are not simple values or objects, but collections. For example, the individual values of a ListBox Web server control are implemented as a collection of ListItem objects.

To set a collection-based control property

  • Instantiate the item you want to use, and then add it to the control's collection.

    The following code example shows how to add a ListItem object to a ListBox control by adding it to the control's Items collection. In the first example, the item is explicitly created before being added. In the second example, items are created and added at the same time.

    Visual BasicВ CopyCode imageCopy Code
    Dim li As ListItem = New ListItem("Item 1")
    ' Create and add the items at the same time
    ListBox1.Items.Add(New ListItem("Apples"))
    ListBox1.Items.Add(New ListItem("Oranges"))
    ListBox1.Items.Add(New ListItem("Lemons"))
    C#В CopyCode imageCopy Code
    // Create the items and then add them to the list.
    ListItem li = new ListItem("Item 1");
    // Create and add the items at the same time.
    ListBox1.Items.Add(new ListItem("Apples"));
    ListBox1.Items.Add(new ListItem("Oranges"));
    ListBox1.Items.Add(new ListItem("Lemons"));

See Also


How to: Set ASP.NET Server Control Properties (Visual Studio)

Other Resources

Setting Web Server Control Properties Programmatically (Visual Studio)

JavaScript Editor jscript editor     Web designer