ListItem 类

表示数据绑定列表控件中的数据项。无法继承此类。

**命名空间:**System.Web.UI.WebControls
**程序集:**System.Web(在 system.web.dll 中)

语法

声明
Public NotInheritable Class ListItem
    Implements IStateManager, IParserAccessor, IAttributeAccessor
用法
Dim instance As ListItem
public sealed class ListItem : IStateManager, IParserAccessor, IAttributeAccessor
public ref class ListItem sealed : IStateManager, IParserAccessor, IAttributeAccessor
public final class ListItem implements IStateManager, IParserAccessor, 
    IAttributeAccessor
public final class ListItem implements IStateManager, IParserAccessor, 
    IAttributeAccessor

备注

ListItem 控件表示 ListBoxRadioButtonList 控件等数据绑定列表控件中的个别数据项。

有几种方法可以指定为列表控件中的项显示的文本。最常用的方法是将文本放在内部 HTML 内容中。内部 HTML 内容是 ListItem 控件的开始标记和结束标记之间的文本。还可以使用 Text 属性指定列表控件中为项显示的文本。

Value 属性使您得以除了在控件中显示的文本外,还可以将值与列表控件中的项关联。例如,可以为列表控件中的项显示文本(如 "Item 1"),并使用 Value 属性为该项指定值,如 "$1.99"

可以设置内部 HTML 内容、TextValue 属性的任意组合。ListItem 控件的结果 HTML 输出取决于所设置的这三种属性的组合。例如,如果所有三种属性按如下设置:

<asp:ListItem Value="Value 1" Text="Item 1">Inner 1</asp:ListItem>

内部 HTML 内容用于呈现的内部 HTML 内容,而 Value 属性用于 Value 属性。结果 HTML 呈现输出为:

<option value="Value 1">Inner 1</option>

下表列出了已设置属性 (Property) 的组合以及用于呈现的内部 HTML 内容和 Value 属性 (Attribute) 的相应属性 (Property)。左边的三列列出了已设置属性的组合。右边的两列列出了用于相应特性的属性值。

内部 HTML 内容

文本属性

值属性

呈现的“内部 HTML”内容

呈现的“值”属性

已设置

已设置

已设置

内部 HTML 内容

值属性

已设置

已设置

未设置

内部 HTML 内容

内部 HTML 内容

已设置

未设置

已设置

内部 HTML 内容

值属性

已设置

未设置

未设置

内部 HTML 内容

“内部 HTML”文本

未设置

已设置

已设置

文本属性

值属性

未设置

已设置

未设置

文本属性

文本属性

未设置

未设置

已设置

值属性

值属性

未设置

未设置

未设置

未设置

未设置

提示

由于 TextValue 属性都具有空字符串默认值,所以列表控件中可能有空列表项。

当显示列表控件时,任何 Selected 属性设置为 trueListItem 控件在此控件中突出显示。

使用 ListItem 控件提供的 Enabled 属性可以指定是启用还是禁用 ListItem 控件。禁用的 ListItem 控件显示为灰色,指示不能选择该控件。使用此属性可禁用 RadioButtonList 控件或 CheckBoxList 控件中的 ListItem 控件。

提示

不能使用此属性禁用 DropDownList 控件或 ListBox 控件中的 ListItem 控件。

有关 ListItem 的实例的初始属性值列表,请参见 ListItem 构造函数。

警告

此控件可用来显示用户输入,而该输入可能包含恶意的客户端脚本。在您的应用程序中显示从客户端发送来的信息之前,先检查这些信息中是否有可执行脚本、SQL 语句或其他代码。可以在将输入文本显示在控件中之前使用验证控件验证用户输入。ASP.NET 提供了输入请求验证功能,可以阻止用户输入中的脚本和 HTML 代码。有关更多信息,请参见 保证标准控件的安全如何:通过对字符串应用 HTML 编码在 Web 应用程序中防止脚本侵入在 ASP.NET 网页中验证用户输入

主题 位置
如何:在列表 Web 服务器控件中添加项 (Visual Studio) 在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:在列表 Web 服务器控件中添加项 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:确定 List Web 服务器控件中的所选内容 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:在集合中设置 Web 服务器控件属性 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:在列表 Web 服务器控件中添加项 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:确定 List Web 服务器控件中的所选内容 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:在集合中设置 Web 服务器控件属性 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:确定 List Web 服务器控件中的所选内容 生成 ASP .NET Web 应用程序
如何:在集合中设置 Web 服务器控件属性 生成 ASP .NET Web 应用程序
如何:在 List Web 服务器控件中添加项 生成 ASP .NET Web 应用程序

示例

下面的示例阐释如何使用 ListBox 控件中的 ListItem 控件。

提示

下面的代码示例使用单文件代码模型;在将这些代码示例直接复制到代码隐藏文件中时,它们可能无法正常工作。必须将每个代码示例都复制到具有 .aspx 扩展名的空文本文件中。有关 Web 窗体代码模型的更多信息,请参见 ASP.NET 网页代码模型

<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
 <head>
 
     <script language="VB" runat="server">
 
         Sub SubmitBtn_Click(Sender As Object, e As EventArgs)
             If ListBox1.SelectedIndex > -1 Then
                 Label1.Text = "You chose: " & ListBox1.SelectedItem.Text
                 Label1.Text &= "<br> with value: " & ListBox1.SelectedItem.Value
             End If
         End Sub
 
     </script>
 
 </head>
 <body>
 
     <h3>ListBox Example</h3>
     <p>
 
     <form runat=server>
 
         <asp:ListBox id=ListBox1 Width="100px" runat="server">
             <asp:ListItem>Item 1</asp:ListItem>
             <asp:ListItem>Item 2</asp:ListItem>
             <asp:ListItem>Item 3</asp:ListItem>
             <asp:ListItem Value="Value 4">Item 4</asp:ListItem>
             <asp:ListItem Text="Item 5" Value="Value 5" Selected="True"/>
             <asp:ListItem>Item 6</asp:ListItem>
         </asp:ListBox>
 
         <asp:button Text="Submit" OnClick="SubmitBtn_Click" runat="server" />
         
         <p>
         
         <asp:Label id=Label1 font-name="Verdana" font-size="10pt" runat="server"/>
         
     </form>
 
 </body>
 </html>
          
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
 <head>
 
     <script language="C#" runat="server">
 
         void SubmitBtn_Click(Object Sender, EventArgs e) {
             if (ListBox1.SelectedIndex > -1) {
                 Label1.Text="You chose: " + ListBox1.SelectedItem.Text;
                 Label1.Text+="<br> with value: " + ListBox1.SelectedItem.Value;
             }
         }
 
     </script>
 
 </head>
 <body>
 
     <h3>ListBox Example</h3>
     <p>
 
     <form runat=server>
 
         <asp:ListBox id=ListBox1 Width="100px" runat="server">
             <asp:ListItem>Item 1</asp:ListItem>
             <asp:ListItem>Item 2</asp:ListItem>
             <asp:ListItem>Item 3</asp:ListItem>
             <asp:ListItem Value="Value 4">Item 4</asp:ListItem>
             <asp:ListItem Text="Item 5" Value="Value 5" Selected="True"/>
             <asp:ListItem>Item 6</asp:ListItem>
         </asp:ListBox>
 
         <asp:button Text="Submit" OnClick="SubmitBtn_Click" runat="server" />
         
         <p>
         
         <asp:Label id=Label1 font-name="Verdana" font-size="10pt" runat="server"/>
         
     </form>
 
 </body>
 </html>
          
<%@ Page Language="JScript" AutoEventWireup="True" %>
<html>
 <head>
 
     <script language="JSCRIPT" runat="server">
 
         function SubmitBtn_Click(Sender : Object, e : EventArgs){
             if(ListBox1.SelectedIndex > -1){
                 Label1.Text = "You chose: " + ListBox1.SelectedItem.Text
                 Label1.Text += "<br> with value: " + ListBox1.SelectedItem.Value
             }
         }
 
     </script>
 
 </head>
 <body>
 
     <h3>ListBox Example</h3>
     <p>
 
     <form runat=server>
 
         <asp:ListBox id=ListBox1 Width="100px" runat="server">
             <asp:ListItem>Item 1</asp:ListItem>
             <asp:ListItem>Item 2</asp:ListItem>
             <asp:ListItem>Item 3</asp:ListItem>
             <asp:ListItem Value="Value 4">Item 4</asp:ListItem>
             <asp:ListItem Text="Item 5" Value="Value 5" Selected="True"/>
             <asp:ListItem>Item 6</asp:ListItem>
         </asp:ListBox>
 
         <asp:button Text="Submit" OnClick="SubmitBtn_Click" runat="server" />
         
         <p>
         
         <asp:Label id=Label1 font-name="Verdana" font-size="10pt" runat="server"/>
         
     </form>
 
 </body>
 </html>
          
<!-- This example demonstrates how to select multiple items from a DataList 
and add the selected items to a DataGrid. The example uses a For Each loop 
to iterate through the ListItem objects in the ListItemCollection of ListBox1. -->


...
<%@ Page language="VB" AutoEventWireup="true"%>
<%@ Import Namespace="System.Data" %>

<HTML>
    <HEAD>
        
        <SCRIPT runat="server">
            ' Global Variables.
            Private dv As DataView
            Private dt As New DataTable()

            Private Sub Page_Load(sender As Object, e As System.EventArgs)
                ' Set the number of rows displayed in the ListBox to be
                ' the number of items in the ListBoxCollection.
                ListBox1.Rows = ListBox1.Items.Count

                ' If the DataTable is already stored in the Web form's default
                ' HttpSessionState variable, then don't recreate the DataTable.
                If Session("data") Is Nothing Then
                    ' Add columns to the DataTable.
                    dt.Columns.Add(New DataColumn("Item"))
                    dt.Columns.Add(New DataColumn("Price"))
        
                    ' Store the DataTable in the Session variable so it can be 
                    ' accessed again later.
                    Session("data") = dt
                    
                    ' Use the table to create a DataView, because the DataGrid
                    ' can only bind to a data source that implements IEnumerable.
                    dv = New DataView(dt)
            
                    ' Set the DataView as the data source, and bind it to the DataGrid.
                    DataGrid1.DataSource = dv
                    DataGrid1.DataBind()
                End If
            End Sub

            Private Sub addButton_Click(sender As Object, e As System.EventArgs)
                ' Add the items selected in ListBox1 to DataGrid1.
                Dim item As ListItem
                For Each item In ListBox1.Items
                    If item.Selected Then
                        ' Add the item to the DataGrid.
                        ' First, get the DataTable from the Session variable.
                        dt = CType(Session("data"), DataTable)
            
                        If  Not (dt Is Nothing) Then
                            ' Create a new DataRow in the DataTable.
                            Dim dr As DataRow
                            dr = dt.NewRow()
                            ' Add the item to the new DataRow.
                            dr("Item") = item.Text
                            ' Add the item's value to the DataRow.
                            dr("Price") = item.Value
                            ' Add the DataRow to the DataTable.
                            dt.Rows.Add(dr)

                            ' Rebind the data to DataGrid1.
                            dv = new DataView(dt)
                            DataGrid1.DataSource = dv
                            DataGrid1.DataBind()
                        End If
                    End If
                Next item
            End Sub
        </SCRIPT>

    </HEAD>
    
    <BODY>
        <form runat="server">

            <h3> ListItemCollection Example </h3>

            <table cellpadding="6" border="0">
                <tr>
                    <td valign="top">
                        <asp:ListBox id="ListBox1" runat="server" SelectionMode="Multiple">
                            <asp:ListItem Value=".89">apples</asp:ListItem>
                            <asp:ListItem Value=".49">bananas</asp:ListItem>
                            <asp:ListItem Value="2.99">cherries</asp:ListItem>
                            <asp:ListItem Value="1.49">grapes</asp:ListItem>
                            <asp:ListItem Value="2.00">mangos</asp:ListItem>
                            <asp:ListItem Value="1.09">oranges</asp:ListItem>
                        </asp:ListBox>
                    </td>

                    <td valign="top">
                        <asp:Button id="addButton" runat="server" Text="Add -->"
                            Width="100px" OnClick="addButton_Click"></asp:Button>
                    </td>

                    <td valign="top">
                        <asp:DataGrid Runat="server" ID="DataGrid1" CellPadding="4">
                        </asp:DataGrid>
                    </td>
                </tr>
            </table>      

        </form>
    </body>
</HTML>
<!-- This example demonstrates how to select multiple items from a DataList and add the 
selected items to a DataGrid. The example uses a foreach loop to iterate through 
the ListItem objects in the ListItemCollection of ListBox1. -->


...
<%@ Page language="c#" AutoEventWireup="true"%>
<%@ Import Namespace="System.Data" %>

<HTML>
    <HEAD>
        
        <SCRIPT language="C#" runat="server">
            // Global Variables.
            private DataView dv;
            private DataTable dt = new DataTable();

            private void Page_Load(object sender, System.EventArgs e)
            {
                // Set the number of rows displayed in the ListBox to be
                // the number of items in the ListBoxCollection.
                ListBox1.Rows = ListBox1.Items.Count;

                // If the DataTable is already stored in the Web form's default
                // HttpSessionState variable, then don't recreate the DataTable.
                if (Session["data"] == null)
                {
                    // Add columns to the DataTable.
                    dt.Columns.Add(new DataColumn("Item"));
                    dt.Columns.Add(new DataColumn("Price"));
        
                    // Store the DataTable in the Session variable so it can 
                    // be accessed again later.
                    Session["data"] = dt;
                    
                    // Use the table to create a DataView, because the DataGrid
                    // can only bind to a data source that implements IEnumerable.
                    dv = new DataView(dt);
            
                    // Set the DataView as the data source, and bind it to the DataGrid.
                    DataGrid1.DataSource = dv;
                    DataGrid1.DataBind();
                }
            }

            private void addButton_Click(object sender, System.EventArgs e)
            {
                // Add the items selected in ListBox1 to DataGrid1.
                foreach (ListItem item in ListBox1.Items)
                {
                    if (item.Selected)
                    {
                        // Add the item to the DataGrid.
                        // First, get the DataTable from the Session variable.
                        dt = (DataTable)Session["data"];
            
                        if (dt != null)
                        { 
                            // Create a new DataRow in the DataTable.
                            DataRow dr = dt.NewRow();
                            // Add the item to the new DataRow.
                            dr["Item"] = item.Text;
                            // Add the item's value to the DataRow.
                            dr["Price"] = item.Value;
                            // Add the DataRow to the DataTable.
                            dt.Rows.Add(dr);

                            // Rebind the data to DataGrid1.
                            dv = new DataView(dt);
                            DataGrid1.DataSource = dv;
                            DataGrid1.DataBind();
                        }
                    }
                }
            }
        </SCRIPT>

    </HEAD>
    
    <BODY>
        <form runat="server">

            <h3> ListItemCollection Example </h3>

            <table cellpadding="6" border="0">
                <tr>
                    <td valign="top">
                        <asp:ListBox id="ListBox1" runat="server" SelectionMode="Multiple">
                            <asp:ListItem Value=".89">apples</asp:ListItem>
                            <asp:ListItem Value=".49">bananas</asp:ListItem>
                            <asp:ListItem Value="2.99">cherries</asp:ListItem>
                            <asp:ListItem Value="1.49">grapes</asp:ListItem>
                            <asp:ListItem Value="2.00">mangos</asp:ListItem>
                            <asp:ListItem Value="1.09">oranges</asp:ListItem>
                        </asp:ListBox>
                    </td>

                    <td valign="top">
                        <asp:Button id="addButton" runat="server" Text="Add -->"
                            Width="100px" OnClick="addButton_Click"></asp:Button>
                    </td>

                    <td valign="top">
                        <asp:DataGrid Runat="server" ID="DataGrid1" CellPadding="4">
                        </asp:DataGrid>
                    </td>
                </tr>
            </table>      

        </form>
    </body>
</HTML>

.NET Framework 安全性

继承层次结构

System.Object
  System.Web.UI.WebControls.ListItem

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

ListItem 成员
System.Web.UI.WebControls 命名空间
ListControl 类
RadioButtonList
ListBox 类
DropDownList 类
CheckBoxList 类

其他资源

ListBox Web 服务器控件
RadioButton 和 RadioButtonList Web 服务器控件概述
BulletedList Web 服务器控件
DropDownList Web 服务器控件
保证标准控件的安全
如何:通过对字符串应用 HTML 编码在 Web 应用程序中防止脚本侵入
在 ASP.NET 网页中验证用户输入