ListItem Costruttori

Definizione

Inizializza una nuova istanza della classe ListItem.

Overload

ListItem()

Inizializza una nuova istanza della classe ListItem.

ListItem(String)

Inizializza una nuova istanza della classe ListItem con i dati di testo specificati.

ListItem(String, String)

Inizializza una nuova istanza della classe ListItem con i dati relativi al testo e al valore specificati.

ListItem(String, String, Boolean)

Inizializza una nuova istanza della classe ListItem con il testo, il valore e i dati abilitati specificati.

ListItem()

Inizializza una nuova istanza della classe ListItem.

public:
 ListItem();
public ListItem ();
Public Sub New ()

Esempio

Nota

L'esempio di codice seguente usa il modello di codice a file singolo e potrebbe non funzionare correttamente se copiato direttamente in un file code-behind. Questo esempio di codice deve essere copiato in un file di testo vuoto con estensione aspx. Per altre informazioni sul modello di codice Web Forms, vedere Web Forms ASP.NET Page Code Model.

<!-- The following example demonstrates adding items to and removing items
from ListBox controls. When an item is selected in ListBox1, a new ListBoxItem with
the same value can be created and added to ListBox2, if ListBox2 does not already 
contain an item with that value. When the new ListBoxItem is created, it receives 
the Value property of the selected item as its Text property, and the Text property
of the selected item as its value property. -->

<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml" >
 <head>
    <title>ListItem Example</title>
<script runat="server">
 
         void AddBtn_Click(Object Sender, EventArgs e) 
         {
             if (ListBox1.SelectedIndex > -1) 
             {
                  if (ListBox2.Items.FindByValue(ListBox1.SelectedItem.Text) == null) 
                 {
                      ListItem Item = new ListItem();
                      // Text and Value are swapped.
                     Item.Text = ListBox1.SelectedItem.Value;
                     Item.Value = ListBox1.SelectedItem.Text;
                     ListBox2.Items.Add(Item);
                  }
             }
         }
 
         void DelBtn_Click(Object Sender, EventArgs e) 
         {
             if (ListBox2.SelectedIndex > -1) 
             {
                 ListBox2.Items.Remove(ListBox2.SelectedItem);
             }
         }
 
     </script>
 
 </head>
 <body>
 
     <h3>ListItem Example</h3>
     <form id="form1" runat="server">
     <table>
     <tr><td>
         <asp:ListBox id="ListBox1" Width="100px" runat="server">
             <asp:ListItem Value="Value 1">Item 1</asp:ListItem>
             <asp:ListItem Value="Value 2">Item 2</asp:ListItem>
             <asp:ListItem Value="Value 3">Item 3</asp:ListItem>
             <asp:ListItem Value="Value 4">Item 4</asp:ListItem>
             <asp:ListItem Value="Value 5" Selected="True">Item 5</asp:ListItem>
             <asp:ListItem Value="Value 6">Item 6</asp:ListItem>
         </asp:ListBox>
     </td><td>
         <asp:button Text="--->" OnClick="AddBtn_Click" runat="server" /><br />
         <asp:button Text="<---" OnClick="DelBtn_Click" runat="server" />
    </td><td>
         <asp:ListBox id="ListBox2" Width="100px" runat="server"/>
     </td></tr>
     </table>
     </form>
 
 </body>
 </html>
<%@ Page Language="VB" %>
<!-- The following example demonstrates adding items to and removing items
from ListBox controls. When an item is selected in ListBox1, a new ListBoxItem with
the same value can be created and added to ListBox2, if ListBox2 does not already 
contain an item with that value. When the new ListBoxItem is created, it receives 
the Value property of the selected item as its Text property, and the Text property
of the selected item as its value property. -->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
 <head>
    <title>ListItem Example</title>
<script language="VB" runat="server">
 
         Sub AddBtn_Click(Sender As Object, e As EventArgs)
             If ListBox1.SelectedIndex > -1 Then
                  If ListBox2.Items.FindByValue(ListBox1.SelectedItem.Text) is Nothing Then
                      Dim Item As ListItem = new ListItem()
                      'Text and Value are swapped
                     Item.Text = ListBox1.SelectedItem.Value
                     Item.Value = ListBox1.SelectedItem.Text
                     ListBox2.Items.Add(Item)
                  End If
             End If
         End Sub
 
         Sub DelBtn_Click(Sender As Object, e As EventArgs)
             If ListBox2.SelectedIndex > -1 Then
                 ListBox2.Items.Remove(ListBox2.SelectedItem)
             End If
         End Sub
 
     </script>
 
 </head>
 <body>
 
     <h3>ListItem Example</h3>
     <form id="form1" runat="server">
     <table>
     <tr><td>
         <asp:ListBox id="ListBox1" Width="100px" runat="server">
             <asp:ListItem Value="Value 1">Item 1</asp:ListItem>
             <asp:ListItem Value="Value 2">Item 2</asp:ListItem>
             <asp:ListItem Value="Value 3">Item 3</asp:ListItem>
             <asp:ListItem Value="Value 4">Item 4</asp:ListItem>
             <asp:ListItem Value="Value 5" Selected="True">Item 5</asp:ListItem>
             <asp:ListItem Value="Value 6">Item 6</asp:ListItem>
         </asp:ListBox>
     </td><td>
         <asp:button Text="--->" OnClick="AddBtn_Click" runat="server" /><br />
         <asp:button Text="<---" OnClick="DelBtn_Click" runat="server" />
    </td><td>
         <asp:ListBox id="ListBox2" Width="100px" runat="server"/>
     </td></tr>
     </table>
     </form>
 
 </body>
 </html>

Commenti

Usare questo costruttore per creare e inizializzare una nuova istanza della ListItem classe usando i valori predefiniti.

Nella tabella seguente vengono illustrati i valori iniziali delle proprietà per un'istanza di ListItem.

Proprietà Valore iniziale
Text null
Value null
Enabled true

Vedi anche

Si applica a

ListItem(String)

Inizializza una nuova istanza della classe ListItem con i dati di testo specificati.

public:
 ListItem(System::String ^ text);
public ListItem (string text);
new System.Web.UI.WebControls.ListItem : string -> System.Web.UI.WebControls.ListItem
Public Sub New (text As String)

Parametri

text
String

Testo da visualizzare nel controllo elenco per l'elemento rappresentato dall'oggetto ListItem.

Esempio

Nota

L'esempio di codice seguente usa il modello di codice a file singolo e potrebbe non funzionare correttamente se copiato direttamente in un file code-behind. Questo esempio di codice deve essere copiato in un file di testo vuoto con estensione aspx. Per altre informazioni sul modello di codice Web Forms, vedere Web Forms ASP.NET Page Code Model.

<!-- The following example demonstrates adding items to and removing items
from ListBox controls. When an item is selected in ListBox1, a new ListBoxItem with
the same value can be created and added to ListBox2, if ListBox2 does not already 
contain an item with that text. -->

<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml" >
 <head>
    <title>ListItem Example</title>
<script runat="server">
 
         void AddBtn_Click(Object Sender, EventArgs e) 
         {
             if (ListBox1.SelectedIndex > -1) 
             {
                  if (ListBox2.Items.FindByText(ListBox1.SelectedItem.Text) == null) 
                 {
                      ListItem Item = new ListItem(ListBox1.SelectedItem.Text);
                     Item.Value = ListBox1.SelectedItem.Value;
                     ListBox2.Items.Add(Item);
                  }
             }
         }
 
         void DelBtn_Click(Object Sender, EventArgs e) 
         {
             if (ListBox2.SelectedIndex > -1) 
             {
                 ListBox2.Items.Remove(ListBox2.SelectedItem);
             }
         }
 
     </script>
 
 </head>
 <body>
 
     <h3>ListItem Example</h3>
     <form id="form1" runat="server">
     <table>
     <tr><td>
         <asp:ListBox id="ListBox1" Width="100px" runat="server">
             <asp:ListItem Value="Value 1">Item 1</asp:ListItem>
             <asp:ListItem Value="Value 2">Item 2</asp:ListItem>
             <asp:ListItem Value="Value 3">Item 3</asp:ListItem>
             <asp:ListItem Value="Value 4">Item 4</asp:ListItem>
             <asp:ListItem Value="Value 5" Selected="True">Item 5</asp:ListItem>
             <asp:ListItem Value="Value 6">Item 6</asp:ListItem>
         </asp:ListBox>
     </td><td>
         <asp:button Text="--->" OnClick="AddBtn_Click" runat="server" /><br />
         <asp:button Text="<---" OnClick="DelBtn_Click" runat="server" />
    </td><td>
         <asp:ListBox id="ListBox2" Width="100px" runat="server"/>
     </td></tr>
     </table>
     </form>
 
 </body>
 </html>
<!-- The following example demonstrates adding items to and removing items
from ListBox controls. When an item is selected in ListBox1, a new ListBoxItem with
the same value can be created and added to ListBox2, if ListBox2 does not already 
contain an item with that text. -->

<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml" >
 <head>
    <title>ListItem Example</title>
<script runat="server">
 
         Sub AddBtn_Click(Sender As Object, e As EventArgs)
             If ListBox1.SelectedIndex > -1 Then
                  If ListBox2.Items.FindByText(ListBox1.SelectedItem.Text) is Nothing Then
                      Dim Item As ListItem = new ListItem(ListBox1.SelectedItem.Text)
                     Item.Value = ListBox1.SelectedItem.Value
                     ListBox2.Items.Add(Item)
                  End If
             End If
         End Sub
 
         Sub DelBtn_Click(Sender As Object, e As EventArgs)
             If ListBox2.SelectedIndex > -1 Then
                 ListBox2.Items.Remove(ListBox2.SelectedItem)
             End If
         End Sub
 
     </script>
 
 </head>
 <body>
 
     <h3>ListItem Example</h3>
     <form id="form1" runat="server">
     <table>
     <tr><td>
         <asp:ListBox id="ListBox1" Width="100px" runat="server">
             <asp:ListItem Value="Value 1">Item 1</asp:ListItem>
             <asp:ListItem Value="Value 2">Item 2</asp:ListItem>
             <asp:ListItem Value="Value 3">Item 3</asp:ListItem>
             <asp:ListItem Value="Value 4">Item 4</asp:ListItem>
             <asp:ListItem Value="Value 5" Selected="True">Item 5</asp:ListItem>
             <asp:ListItem Value="Value 6">Item 6</asp:ListItem>
         </asp:ListBox>
     </td><td>
         <asp:button Text="--->" OnClick="AddBtn_Click" runat="server" /><br />
         <asp:button Text="<---" OnClick="DelBtn_Click" runat="server" />
    </td><td>
         <asp:ListBox id="ListBox2" Width="100px" runat="server"/>
     </td></tr>
     </table>
     </form>
 
 </body>
 </html>

Commenti

Utilizzare questo costruttore per creare e inizializzare una nuova istanza della ListItem classe usando il testo specificato.

Nella tabella seguente vengono illustrati i valori iniziali delle proprietà per un'istanza di ListItem.

Proprietà Valore iniziale
Text Valore del parametro text.
Value null
Enabled true

Vedi anche

Si applica a

ListItem(String, String)

Inizializza una nuova istanza della classe ListItem con i dati relativi al testo e al valore specificati.

public:
 ListItem(System::String ^ text, System::String ^ value);
public ListItem (string text, string value);
new System.Web.UI.WebControls.ListItem : string * string -> System.Web.UI.WebControls.ListItem
Public Sub New (text As String, value As String)

Parametri

text
String

Testo da visualizzare nel controllo elenco per l'elemento rappresentato dall'oggetto ListItem.

value
String

Valore associato all'oggetto ListItem.

Esempio

Nota

L'esempio di codice seguente usa il modello di codice a file singolo e potrebbe non funzionare correttamente se copiato direttamente in un file code-behind. Questo esempio di codice deve essere copiato in un file di testo vuoto con estensione aspx. Per altre informazioni sul modello di codice Web Forms, vedere Web Forms ASP.NET Page Code Model.

<!-- The following example demonstrates adding items to and removing items
from ListBox controls. When an item is selected in ListBox1, a new ListBoxItem with
the same value can be created and added to ListBox2, if ListBox2 does not already 
contain an item with that text. -->

<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml" >
 <head>
    <title>ListItem Example</title>
<script runat="server">
 
         void AddBtn_Click(Object Sender, EventArgs e) 
         {
             if (ListBox1.SelectedIndex > -1) 
             {
                  if (ListBox2.Items.FindByText(ListBox1.SelectedItem.Text) == null) 
                 {
                     ListItem Item = new ListItem(ListBox1.SelectedItem.Text, 
                         ListBox1.SelectedItem.Value);
                     ListBox2.Items.Add(Item);
                  }
             }
         }
 
         void DelBtn_Click(Object Sender, EventArgs e) 
         {
             if (ListBox2.SelectedIndex > -1) 
             {
                 ListBox2.Items.Remove(ListBox2.SelectedItem);
             }
         }
 
     </script>
 
 </head>
 <body>
 
     <h3>ListItem Example</h3>
     <form id="form1" runat="server">
     <table>
     <tr><td>
         <asp:ListBox id="ListBox1" Width="100px" runat="server">
             <asp:ListItem Value="Value 1">Item 1</asp:ListItem>
             <asp:ListItem Value="Value 2">Item 2</asp:ListItem>
             <asp:ListItem Value="Value 3">Item 3</asp:ListItem>
             <asp:ListItem Value="Value 4">Item 4</asp:ListItem>
             <asp:ListItem Value="Value 5" Selected="True">Item 5</asp:ListItem>
             <asp:ListItem Value="Value 6">Item 6</asp:ListItem>
         </asp:ListBox>
     </td><td>
         <asp:button Text="--->" OnClick="AddBtn_Click" runat="server" /><br />
         <asp:button Text="<---" OnClick="DelBtn_Click" runat="server" />
    </td><td>
         <asp:ListBox id="ListBox2" Width="100px" runat="server"/>
     </td></tr>
     </table>
     </form>
 
 </body>
 </html>
<!-- The following example demonstrates adding items to and removing items
from ListBox controls. When an item is selected in ListBox1, a new ListBoxItem with
the same value can be created and added to ListBox2, if ListBox2 does not already 
contain an item with that text. -->

<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml" >
 <head>
    <title>ListItem Example</title>
<script runat="server">
 
         Sub AddBtn_Click(Sender As Object, e As EventArgs)
             If ListBox1.SelectedIndex > -1 Then
                  If ListBox2.Items.FindByText(ListBox1.SelectedItem.Text) is Nothing Then
                      Dim Item As ListItem = new ListItem(ListBox1.SelectedItem.Text, _
                            ListBox1.SelectedItem.Value)
                     ListBox2.Items.Add(Item)
                  End If
             End If
         End Sub
 
         Sub DelBtn_Click(Sender As Object, e As EventArgs)
             If ListBox2.SelectedIndex > -1 Then
                 ListBox2.Items.Remove(ListBox2.SelectedItem)
             End If
         End Sub
 
     </script>
 
 </head>
 <body>
 
     <h3>ListItem Example</h3>
     <form id="form1" runat="server">
     <table>
     <tr><td>
         <asp:ListBox id="ListBox1" Width="100px" runat="server">
             <asp:ListItem Value="Value 1">Item 1</asp:ListItem>
             <asp:ListItem Value="Value 2">Item 2</asp:ListItem>
             <asp:ListItem Value="Value 3">Item 3</asp:ListItem>
             <asp:ListItem Value="Value 4">Item 4</asp:ListItem>
             <asp:ListItem Value="Value 5" Selected="True">Item 5</asp:ListItem>
             <asp:ListItem Value="Value 6">Item 6</asp:ListItem>
         </asp:ListBox>
     </td><td>
         <asp:button Text="--->" OnClick="AddBtn_Click" runat="server" /><br />
         <asp:button Text="<---" OnClick="DelBtn_Click" runat="server" />
    </td><td>
         <asp:ListBox id="ListBox2" Width="100px" runat="server"/>
     </td></tr>
     </table>
     </form>
 
 </body>
 </html>

Commenti

Utilizzare questo costruttore per creare e inizializzare una nuova istanza della ListItem classe utilizzando il testo e il valore specificati.

Nella tabella seguente vengono illustrati i valori iniziali delle proprietà per un'istanza di ListItem.

Proprietà Valore iniziale
Text Valore del parametro text.
Value Valore del parametro value.
Enabled true

Vedi anche

Si applica a

ListItem(String, String, Boolean)

Inizializza una nuova istanza della classe ListItem con il testo, il valore e i dati abilitati specificati.

public:
 ListItem(System::String ^ text, System::String ^ value, bool enabled);
public ListItem (string text, string value, bool enabled);
new System.Web.UI.WebControls.ListItem : string * string * bool -> System.Web.UI.WebControls.ListItem
Public Sub New (text As String, value As String, enabled As Boolean)

Parametri

text
String

Testo da visualizzare nel controllo elenco per l'elemento rappresentato dall'oggetto ListItem.

value
String

Valore associato all'oggetto ListItem.

enabled
Boolean

Indica se l'oggetto ListItem è abilitato.

Esempio

Nell'esempio seguente viene illustrato come aggiungere e rimuovere elementi dai ListBox controlli. Quando un elemento viene selezionato nel ListBox1 controllo, è possibile creare e aggiungere un nuovo ListItem controllo con lo stesso valore al ListBox2 controllo, se il ListBox2 controllo non contiene già un elemento con tale testo. In questo esempio il costruttore viene chiamato con enabled impostato su true. Se è stato chiamato con enabled impostato su false, il nuovo ListItem controllo non verrà visualizzato nel ListBox controllo .

Nota

L'esempio di codice seguente usa il modello di codice a file singolo e potrebbe non funzionare correttamente se copiato direttamente in un file code-behind. Questo esempio di codice deve essere copiato in un file di testo vuoto con estensione aspx. Per altre informazioni sul modello di codice Web Forms, vedere Web Forms ASP.NET Page Code Model.


<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml" >
 <head>
    <title>ListItem Example</title>
<script runat="server">
 
         void AddBtn_Click(Object Sender, EventArgs e) 
         {
             if (ListBox1.SelectedIndex > -1) 
             {
                  if (ListBox2.Items.FindByText(ListBox1.SelectedItem.Text) == null) 
                 {
                     ListItem Item = new ListItem(ListBox1.SelectedItem.Text, 
                         ListBox1.SelectedItem.Value, true);
                     ListBox2.Items.Add(Item);
                  }
             }
         }
 
         void DelBtn_Click(Object Sender, EventArgs e) 
         {
             if (ListBox2.SelectedIndex > -1) 
             {
                 ListBox2.Items.Remove(ListBox2.SelectedItem);
             }
         }
 
     </script>
 
 </head>
 <body>
 
     <h3>ListItem Example</h3>
     <form id="form1" runat="server">
     <table>
     <tr><td>
         <asp:ListBox id="ListBox1" Width="100px" runat="server">
             <asp:ListItem Value="Value 1">Item 1</asp:ListItem>
             <asp:ListItem Value="Value 2">Item 2</asp:ListItem>
             <asp:ListItem Value="Value 3">Item 3</asp:ListItem>
             <asp:ListItem Value="Value 4">Item 4</asp:ListItem>
             <asp:ListItem Value="Value 5" Selected="True">Item 5</asp:ListItem>
             <asp:ListItem Value="Value 6">Item 6</asp:ListItem>
         </asp:ListBox>
     </td><td>
         <asp:button Text="--->" OnClick="AddBtn_Click" runat="server" /><br />
         <asp:button Text="<---" OnClick="DelBtn_Click" runat="server" />
    </td><td>
         <asp:ListBox id="ListBox2" Width="100px" runat="server"/>
     </td></tr>
     </table>
     </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">
<html xmlns="http://www.w3.org/1999/xhtml" >
 <head>
    <title>ListItem Example</title>
<script runat="server">
 
         Sub AddBtn_Click(Sender As Object, e As EventArgs)
             If ListBox1.SelectedIndex > -1 Then
                  If ListBox2.Items.FindByText(ListBox1.SelectedItem.Text) is Nothing Then
             Dim Item As ListItem = New ListItem(ListBox1.SelectedItem.Text, _
                         ListBox1.SelectedItem.Value, True)
                     ListBox2.Items.Add(Item)
                  End If
             End If
         End Sub
 
         Sub DelBtn_Click(Sender As Object, e As EventArgs)
             If ListBox2.SelectedIndex > -1 Then
                 ListBox2.Items.Remove(ListBox2.SelectedItem)
             End If
         End Sub
 
     </script>
 
 </head>
 <body>
 
     <h3>ListItem Example</h3>
     <form id="form1" runat="server">
     <table>
     <tr><td>
         <asp:ListBox id="ListBox1" Width="100px" runat="server">
             <asp:ListItem Value="Value 1">Item 1</asp:ListItem>
             <asp:ListItem Value="Value 2">Item 2</asp:ListItem>
             <asp:ListItem Value="Value 3">Item 3</asp:ListItem>
             <asp:ListItem Value="Value 4">Item 4</asp:ListItem>
             <asp:ListItem Value="Value 5" Selected="True">Item 5</asp:ListItem>
             <asp:ListItem Value="Value 6">Item 6</asp:ListItem>
         </asp:ListBox>
     </td><td>
         <asp:button Text="--->" OnClick="AddBtn_Click" runat="server" /><br />
         <asp:button Text="<---" OnClick="DelBtn_Click" runat="server" />
    </td><td>
         <asp:ListBox id="ListBox2" Width="100px" runat="server"/>
     </td></tr>
     </table>
     </form>
 
 </body>
 </html>

Commenti

Nella tabella seguente vengono illustrati i valori iniziali delle proprietà per un'istanza di ListItem.

Proprietà Valore iniziale
Text Valore del parametro text.
Value Valore del parametro value.
Enabled Valore del parametro enabled.

Vedi anche

Si applica a