Export (0) Print
Expand All

Adding Items in a List Web Server Control

The information in this topic applies to all list Web server controls: ListBox, DropDownList, CheckBoxList, and RadioButtonList.

You can add items to a list Web server control in these ways:

When you add a list item, you specify up to three possible properties of the item:

PropertyDescription
TextThe text displayed in the list.
ValueThe value associated with an item. Setting this property allows you to associate a value with a specific item without displaying it. For example, you can set the Text property to the name of chemical element and the Value property to the symbol of the element.
SelectedA Boolean value indicating whether the item is selected. In a CheckBoxList control and in a multi-selection ListBox control, more than one item can be selected. In a DropDownList control, RadioButtonList control, and single-selection ListBox control, only one item can be selected at a time. If you set more than one selected item in these controls, the browser determines which item is rendered as selected.

To add static items at design time

  1. In Design view, select the list control. In the Properties window, click the ellipsis button (w384x40a.vbellipsesbutton(en-us,VS.71).gif) for the Items property to open the Collection Editor dialog box.
  2. Click Add to add a new item.
  3. To set the properties for the new item, select it, and then in the ListItem Properties list, enter values for its Text, Value, and Selected properties.
  4. Repeat Steps 2 and 3 for each item to add, and then choose OK to close the Collection Editor dialog box.

To add items programmatically

  1. Create a new object of type ListItem and set its Text and Value properties.
  2. Call the Add method of the control's Items collection and pass it the new object.

    The following example shows how to add ListItem objects to a ListBox control, but the procedure is identical for all list Web server controls.

    ' Visual Basic
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Me.ListBox1.Items.Add(New ListItem("Carbon", "C"))
       Me.ListBox1.Items.Add(New ListItem("Oxygen", "O"))
    End Sub
    
    // C#
    public void Button1_Click (object sender, System.EventArgs e)
    {
       this.ListBox1.Items.Add(new ListItem("Carbon", "C"));
       this.ListBox1.Items.Add(new ListItem("Oxygen", "O"));
    }
    

    The following example shows how you can create new items programmatically. Using a loop, the code creates three new items in a ListBox control.

    ' Visual Basic
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       Dim elements() As String = New String() {"Carbon", "Oxygen", "Hydrogen"}
       Dim i As Integer
       For i = 0 To ubound(elements)
          Me.ListBox1.Items.Add(New ListItem(elements(i)))
       Next
    End Sub
    
    // C#
    public void Button1_Click (object sender, System.EventArgs e)
    {
       string[] elements = {"Carbon", "Oxygen", "Hydrogen"};
       int i;
    
       for (i = 0;i < elements.GetLength(0);i++)
       {
          this.ListBox1.Items.Add(new ListItem(elements[i]));
       }
    }
    

See Also

Populating a List Web Server Control from a Database | Determining the Selection in a List Web Server Control | Setting the Selection in a List Web Server Control | ListBox Class

Show:
© 2014 Microsoft