HtmlSelect.Items Proprietà

Definizione

Ottiene un insieme contenente tutti gli elementi elencati in un controllo HtmlSelect.

public:
 property System::Web::UI::WebControls::ListItemCollection ^ Items { System::Web::UI::WebControls::ListItemCollection ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Web.UI.WebControls.ListItemCollection Items { get; }
[<System.ComponentModel.Browsable(false)>]
member this.Items : System.Web.UI.WebControls.ListItemCollection
Public ReadOnly Property Items As ListItemCollection

Valore della proprietà

ListItemCollection contenente gli elementi elencati in un controllo HtmlSelect.

Attributi

Esempio

Nell'esempio di codice seguente viene illustrato come utilizzare la Items raccolta per scorrere gli elementi nel HtmlSelect controllo e determinare quali elementi sono selezionati.


<%@ Page Language="C#" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    void Button_Click (Object sender, EventArgs e)
    {
        
        Label1.Text = "You selected:";

        for (int i = 0; i <= Select1.Items.Count - 1; i++)
        {
           if (Select1.Items[i].Selected)
              Label1.Text += "<br />    -" + Select1.Items[i].Text;
        }

    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head>
    <title> HtmlSelect Example </title>
</head>
<body>
<form id="form1" runat="server">
    
    <h3> HtmlSelect Example </h3>

    Select items from the list: <br /><br />

    <select id="Select1" 
            multiple="true"
            runat="server">

        <option value="1" selected="selected"> Item 1 </option>
        <option value="2"> Item 2 </option>
        <option value="3"> Item 3 </option>
        <option value="4"> Item 4 </option>
        <option value="5"> Item 5 </option>
        <option value="6"> Item 6 </option>

    </select>
    <br /><br />

    <button id="Button1"
            onserverclick="Button_Click"
            runat="server">

        Submit

    </button>
    <br /><br />

    <asp:Label id="Label1"
         runat="server"/>

   </form>
</body>
</html>

<%@ Page Language="VB" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    Sub Button_Click (sender As Object, e As EventArgs)
        
        Dim i As Integer

        Label1.Text = "You selected:"
        For i = 0 to Select1.Items.Count - 1
  
            If Select1.Items(i).Selected Then
               Label1.Text = Label1.Text & "<br />    -" _
                   & Select1.Items(i).Text
            End If         

        Next i

    End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head>
    <title> HtmlSelect Example </title>
</head>
<body>
<form id="form1" runat="server">

    <h3> HtmlSelect Example </h3>

    Select items from the list: <br /><br />

    <select id="Select1" 
            multiple="true"
            runat="server">

        <option value="1" selected="selected"> Item 1 </option>
        <option value="2"> Item 2 </option>
        <option value="3"> Item 3 </option>
        <option value="4"> Item 4 </option>
        <option value="5"> Item 5 </option>
        <option value="6"> Item 6 </option>

    </select>
    <br /><br />

    <button id="Button1"
            onserverclick="Button_Click"
            runat="server">

        Submit

    </button>
    <br /><br />

    <asp:Label id="Label1"
         runat="server"/>

   </form>
</body>
</html>

Commenti

Utilizzare la Items raccolta per gestire gli elementi elencati nel HtmlSelect controllo . È possibile aggiungere elementi a livello di codice, rimuovere elementi da e inserire elementi nella raccolta.

La Items raccolta viene comunemente usata per scorrere gli elementi nel HtmlSelect controllo . Ad esempio, quando vengono selezionati più elementi, è possibile scorrere la Items raccolta per determinare quali elementi sono selezionati.

Si applica a

Vedi anche