ListItem 构造函数

定义

初始化 ListItem 类的新实例。

重载

ListItem()

初始化 ListItem 类的新实例。

ListItem(String)

使用指定的文本数据初始化 ListItem 类的新实例。

ListItem(String, String)

使用指定的文本和值数据初始化 ListItem 类的新实例。

ListItem(String, String, Boolean)

使用指定的文本、值和启用的数据初始化 ListItem 类的新实例。

ListItem()

初始化 ListItem 类的新实例。

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

示例

备注

下面的代码示例使用单文件代码模型,如果直接复制到代码隐藏文件中,可能无法正常工作。 必须将此代码示例复制到扩展名为 .aspx 的空文本文件中。 有关Web Forms代码模型的详细信息,请参阅 ASP.NET Web Forms页代码模型

<!-- 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>

注解

使用此构造函数使用默认值创建和初始化 类的新实例 ListItem

下表显示了 实例 ListItem的初始属性值。

属性 初始值
Text null
Value null
Enabled true

另请参阅

适用于

ListItem(String)

使用指定的文本数据初始化 ListItem 类的新实例。

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)

参数

text
String

要在列表控件中为 ListItem 所表示的项显示的文本。

示例

注意

下面的代码示例使用单文件代码模型,如果直接复制到代码隐藏文件中,可能无法正常工作。 必须将此代码示例复制到扩展名为 .aspx 的空文本文件中。 有关Web Forms代码模型的详细信息,请参阅 ASP.NET Web Forms页代码模型

<!-- 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>

注解

使用此构造函数使用指定的文本创建和初始化 类的新实例 ListItem

下表显示了 实例 ListItem的初始属性值。

属性 初始值
Text text 参数的值。
Value null
Enabled true

另请参阅

适用于

ListItem(String, String)

使用指定的文本和值数据初始化 ListItem 类的新实例。

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)

参数

text
String

要在列表控件中为 ListItem 所表示的项显示的文本。

value
String

ListItem 关联的值。

示例

备注

下面的代码示例使用单文件代码模型,如果直接复制到代码隐藏文件中,可能无法正常工作。 必须将此代码示例复制到扩展名为 .aspx 的空文本文件中。 有关Web Forms代码模型的详细信息,请参阅 ASP.NET Web Forms页代码模型

<!-- 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>

注解

使用此构造函数使用指定的文本和值创建和初始化 类的新实例 ListItem

下表显示了 实例 ListItem的初始属性值。

属性 初始值
Text text 参数的值。
Value value 参数的值。
Enabled true

另请参阅

适用于

ListItem(String, String, Boolean)

使用指定的文本、值和启用的数据初始化 ListItem 类的新实例。

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)

参数

text
String

要在列表控件中为 ListItem 所表示的项显示的文本。

value
String

ListItem 关联的值。

enabled
Boolean

指示是否启用了 ListItem

示例

以下示例演示如何向控件添加项以及从 ListBox 控件中删除项。 在控件中选择ListBox1项时,如果ListBox2控件尚未包含具有该文本的项,则可以创建具有相同值的新ListItem控件并将其添加到ListBox2控件中。 在此示例中,调用构造函数,并将 enabled 设置为 true。 如果调用 enabled 时将 设置为 false,则新 ListItem 控件将不会显示在 控件中 ListBox

备注

下面的代码示例使用单文件代码模型,如果直接复制到代码隐藏文件中,可能无法正常工作。 必须将此代码示例复制到扩展名为 .aspx 的空文本文件中。 有关Web Forms代码模型的详细信息,请参阅 ASP.NET Web Forms页代码模型


<%@ 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>

注解

下表显示了 实例 ListItem的初始属性值。

属性 初始值
Text text 参数的值。
Value value 参数的值。
Enabled enabled 参数的值。

另请参阅

适用于