Compartilhar via


Como: Adicionar itens em controles de lista do servidor Web

As informações contidas neste tópico se aplicam aos seguintes controles de servidores Web:

Você pode adicionar itens a um controle de servidor Web lista de três maneiras a seguir:

Quando você adiciona um item da lista, você especifica até três propriedades possíveis do item.The following table describes these properties.

Propriedade

Descrição

Text

Especifica o texto exibido na lista.

Value

Specifies a value that is associated with an item but is not displayed.For example, you can set the Text property to the name of chemical element and the Value property to the symbol of the element.

Selected

Indicates 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.Se voc definir mais de um item selecionado nesses controles, o navegador determina qual item é processado como marcado.In a BulletedList control, this setting has no effect.

Para adicionar itens estáticos em tempo de design

  1. No modo Design, selecione o controle lista ao qual você deseja adicionar itens.

    In the Properties window, click the ellipsis button () in the Items box.

    A caixa de diálogo ListItem Collection Editor aparece.

  2. Clique em Add para adicionar um novo item.

  3. Select the new item, and then in the properties grid, enter values for its Text, Value, and Selected properties.

  4. Repeat steps 2 and 3 for each item you want to add, and then click OK.

Para adicionar itens programaticamente

  1. Create a new object of type ListItem and set its Text and Value properties.Typically, you create the new ListItem by calling the Add method.

  2. Call the Add method of the control's Items collection and pass it the new object.

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

    Protected Sub Button1_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles Button1.Click
        ListBox1.Items.Add(New ListItem("Carbon", "C"))
        ListBox1.Items.Add(New ListItem("Oxygen", "O"))
    End Sub
    
    Protected void Button1_Click (object sender, System.EventArgs e)
    {
        ListBox1.Items.Add(new ListItem("Carbon", "C"));
        ListBox1.Items.Add(new ListItem("Oxygen", "O"));
    }
    

Consulte também

Tarefas

Como: popular a lista servidor Web controles a partir de fontes de dados