Esta página foi útil?
Seus comentários sobre este conteúdo são importantes. Queremos saber sua opinião.
Comentários adicionais?
1500 caracteres restantes
Exportar (0) Imprimir
Expandir Tudo
Este artigo foi traduzido por máquina. Coloque o ponteiro do mouse sobre as frases do artigo para ver o texto original. Mais informações.
Tradução
Original

ListItemCollection Classe

A collection of ListItem objects in a list control. Esta classe não pode ser herdada.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (em System.Web. dll)

[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public sealed class ListItemCollection : IList, 
	ICollection, IEnumerable, IStateManager

The ListItemCollection class represents a collection of ListItem objects.The ListItem objects, in turn, represent the items displayed in list controls, such as the ListBox.To programmatically retrieve ListItem objects from a list control, use one of following methods:

  • Use the indexer to get a single ListItem from the collection, using array notation.

  • Use the CopyTo method to copy the contents of the collection to a System.Array object, which can then be used to get items from the collection.

  • Use the GetEnumerator method to create a System.Collections.IEnumerator implemented object, which can then be used to get items from the collection.

  • Use foreach (C#) or For Each (Visual Basic) to iterate through the collection.

The Count property specifies the total number of items in the collection, and is commonly used to determine the upper bound of the collection.You can add and remove items from the collection by using the Add and Remove methods.

The following code example demonstrates creating ListItemCollection objects, adding items to the collections, and removing items from the collections.In the example, the ListItemCollection named listBoxData is used as the data source for a ListBox control called ListBox1, and the ListItemCollection called ddBoxData is used as the data source for a DropDownList control called DropDownList1.

<%@ Page language= AutoEventWireup= %>
<%@ Import Namespace= %>

<!DOCTYPE html PUBLIC "-
    "http:
<script runat=>
      Page_Load(object sender, System.EventArgs e)
    {
        
        
         (!IsPostBack)
        {
            
            ListItemCollection listBoxData =  ListItemCollection();
            
            listBoxData.Add( ListItem());
            listBoxData.Add( ListItem());
            listBoxData.Add( ListItem());
            listBoxData.Add();
            listBoxData.Add();
            listBoxData.Add();
            
            ListBox1.DataSource = listBoxData;
            ListBox1.DataBind();

            
            ListItemCollection ddBoxData =  ListItemCollection();
            
            DropDownList1.DataSource = ddBoxData;
            DropDownList1.DataBind();
        }        
    }

      moveButton1_Click(object sender, System.EventArgs e)
    {
        
        
        DropDownList1.SelectedIndex = -1;
        
        DropDownList1.Items.Add(ListBox1.SelectedItem);
        
        ListBox1.Items.Remove(ListBox1.SelectedItem);
    }

      moveButton2_Click(object sender, System.EventArgs e)
    {
        
        
        ListBox1.SelectedIndex = -1;
        
        ListBox1.Items.Add(DropDownList1.SelectedItem);
        
        DropDownList1.Items.Remove(DropDownList1.SelectedItem);
    }
</script>
<html xmlns="http:
    <head>
    <title>ASP.NET Example</title>
</head>

    <body>
        <form id= runat=>
            <table cellpadding= border=>
                <tr>
                    <td rowspan= valign=>
                        <asp:ListBox id= runat= 
                            SelectionMode= Width=>
                        </asp:ListBox>
                    </td>

                    <td>
                        <asp:Button id= runat= Text=
                            Width= OnClick=></asp:Button>
                    </td>

                    <td rowspan= valign=>
                        <asp:DropDownList Runat= ID=
                            Width=>
                        </asp:DropDownList>
                    </td>
                </tr>

                <tr>
                    <td>
                        <asp:Button ID= Runat= Text=
                            Width= onClick=></asp:Button>
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>


<!-- This example demonstrates how to  multiple items  a DataList and add the 
selected items to a DataGrid. The example uses a  loop to iterate through 
the ListItem objects  the ListItemCollection of ListBox1. -->



<%@ Page language= AutoEventWireup=%>
<%@ Import Namespace= %>

<!DOCTYPE html PUBLIC "-
    "http:
<script language= runat=>
            
             DataView dv;
             DataTable dt =  DataTable();

              Page_Load(object sender, System.EventArgs e)
            {
                
                
                ListBox1.Rows = ListBox1.Items.Count;

                
                
                 (Session[] == )
                {
                    
                    dt.Columns.Add( DataColumn());
                    dt.Columns.Add( DataColumn());
            
                    
                    Session[] = dt;

                    
                    
                    dv =  DataView(dt);

                    
                    DataGrid1.DataSource = dv;
                    DataGrid1.DataBind();
                }
            }

              addButton_Click(object sender, System.EventArgs e)
            {
                
                 (ListItem item  ListBox1.Items)
                {
                     (item.Selected)
                    {
                        
                        
                        dt = (DataTable)Session[];

                         (dt != )
                        { 
                            
                            DataRow dr = dt.NewRow();
                            
                            dr[] = item.Text;
                            
                            dr[] = item.Value;
                            
                            dt.Rows.Add(dr);

                            
                            dv =  DataView(dt);
                            DataGrid1.DataSource = dv;
                            DataGrid1.DataBind();
                        }
                    }
                }
            }
        </script>
<html xmlns="http:
    <head>
    <title> ListItemCollection Example </title>
</head>

    <body>
        <form id= runat=>

            <h3> ListItemCollection Example </h3>

            <table cellpadding= border=>
                <tr>
                    <td valign=>
                        <asp:ListBox id= runat= SelectionMode=>
                            <asp:ListItem Value=>apples</asp:ListItem>
                            <asp:ListItem Value=>bananas</asp:ListItem>
                            <asp:ListItem Value=>cherries</asp:ListItem>
                            <asp:ListItem Value=>grapes</asp:ListItem>
                            <asp:ListItem Value=>mangos</asp:ListItem>
                            <asp:ListItem Value=>oranges</asp:ListItem>
                        </asp:ListBox>
                    </td>

                    <td valign=>
                        <asp:Button id= runat= Text=
                            Width= OnClick=></asp:Button>
                    </td>

                    <td valign=>
                        <asp:DataGrid Runat= ID= CellPadding=>
                        </asp:DataGrid>
                    </td>
                </tr>
            </table>        
        </form>
    </body>
</html>


System.Object
  System.Web.UI.WebControls.ListItemCollection

Quaisquer membros públicos estático (compartilhados na Visual Basic) desse tipo são Thread seguro. Não há garantia de que qualquer membro de instância seja isento de segmentos.

Contribuições da comunidade

ADICIONAR
A Microsoft está realizando uma pesquisa online para saber sua opinião sobre o site do MSDN. Se você optar por participar, a pesquisa online lhe será apresentada quando você sair do site do MSDN.

Deseja participar?
Mostrar:
© 2015 Microsoft