ListItem 类

定义

表示数据绑定列表控件中的数据项。 此类不能被继承。

public ref class ListItem sealed : System::Web::UI::IAttributeAccessor, System::Web::UI::IParserAccessor, System::Web::UI::IStateManager
[System.ComponentModel.TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
public sealed class ListItem : System.Web.UI.IAttributeAccessor, System.Web.UI.IParserAccessor, System.Web.UI.IStateManager
[<System.ComponentModel.TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))>]
type ListItem = class
    interface IStateManager
    interface IParserAccessor
    interface IAttributeAccessor
Public NotInheritable Class ListItem
Implements IAttributeAccessor, IParserAccessor, IStateManager
继承
ListItem
属性
实现

示例

以下示例演示了控件在控件中的ListBox用法ListItem

注意

以下代码示例使用单文件代码模型,如果直接复制到代码隐藏文件中,可能无法正常工作。 必须将每个代码示例复制到扩展名为 .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>ListBox Example</title>
<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>
     <br />
 
     <form id="form1" 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" />
         
         <br />
         
         <asp:Label id="Label1" font-names="Verdana" font-size="10pt" 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">
<html xmlns="http://www.w3.org/1999/xhtml" >
 <head>
    <title>ListBox Example</title>
<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>
     <br />
 
     <form id="form1" 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" />
         
         <br />
         
         <asp:Label id="Label1" font-names="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 foreach loop to iterate through 
the ListItem objects in the ListItemCollection of ListBox1. -->
<!-- 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="c#" AutoEventWireup="true"%>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script language="C#" runat="server">
            // Global Variables.
            private DataView dv;
            private DataTable dt = new DataTable();

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

                // 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)
            {
// <Snippet5>
                // 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);
// </Snippet5>

                            // Rebind the data to DataGrid1.
                            dv = new DataView(dt);
                            DataGrid1.DataSource = dv;
                            DataGrid1.DataBind();
                        }
                    }
                }
            }
        </script>
<html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
    <title> ListItemCollection Example </title>
</head>
    
    <body>
        <form id="form1" 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>
<%@ Page language="VB" AutoEventWireup="true"%>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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)
' <Snippet4>
                ' Set the number of rows displayed in the ListBox to be
                ' the number of items in the ListBoxCollection.
                ListBox1.Rows = ListBox1.Items.Count
' </Snippet4>

                ' 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)
' <Snippet5>
                ' 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)
' </Snippet5>

                            ' Rebind the data to DataGrid1.
                            dv = new DataView(dt)
                            DataGrid1.DataSource = dv
                            DataGrid1.DataBind()
                        End If
                    End If
                Next item
            End Sub
        </script>
<html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
    <title> ListItemCollection Example </title>
</head>
    
    <body>
        <form id="form1" 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>

注解

控件 ListItem 表示数据绑定列表控件中的单个数据项,例如 ListBoxRadioButtonList 控件。

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

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

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

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

内部 HTML 内容用于呈现的内部 HTML 内容, Value 属性用于 Value 属性。 生成的 HTML 呈现输出为:

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

下表列出了集属性和用于呈现的内部 HTML 内容和 Value 属性的相应属性的组合。 左侧的三列列出了集属性的组合。 右侧列表上的两列,属性值用于相应的属性。

内部 HTML 内容 Text 属性 Value 属性 呈现的内部 HTML 内容 呈现的值属性
设置 设置 设置 内部 HTML 内容 Value 属性
设置 设置 未设置 内部 HTML 内容 内部 HTML 内容
设置 未设置 设置 内部 HTML 内容 Value 属性
设置 未设置 未设置 内部 HTML 内容 内部 HTML 文本
未设置 设置 设置 Text 属性 Value 属性
未设置 设置 未设置 Text 属性 Text 属性
未设置 未设置 设置 Value 属性 Value 属性
未设置 未设置 未设置 未设置 未设置

注意

Text由于 和 Value 属性均具有空字符串的默认值,因此列表控件中可能具有空列表项。

显示列表控件时,其属性设置为 true 的任何ListItem控件Selected在控件中突出显示。

控件 ListItem 提供 Enabled 属性,用于指定控件是启用还是 ListItem 禁用。 ListItem禁用的控件灰显,以指示无法选择它。 使用此属性可禁用ListItem控件或CheckBoxList控件中的RadioButtonList控件。

注意

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

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

注意

此控件可用于显示用户输入,其中可能包括恶意客户端脚本。 在应用程序中显示之前,请检查从客户端发送的任何信息,以获取可执行脚本、SQL 语句或其他代码。 在控件中显示输入文本之前,可以使用验证控件来验证用户输入。 ASP.NET 提供输入请求验证功能,以阻止用户输入中的脚本和 HTML。 有关详细信息,请参阅保护标准控件如何:通过对字符串应用 HTML 编码在 Web 应用程序中防止脚本攻击在 ASP.NET 网页 中验证用户输入

构造函数

ListItem()

初始化 ListItem 类的新实例。

ListItem(String)

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

ListItem(String, String)

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

ListItem(String, String, Boolean)

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

属性

Attributes

获取类不直接支持的 ListItem 的特性名和值对的集合。

Enabled

获取或设置指示是否启用了列表项的值。

Selected

获取或设置指示是否选定了项的值。

Text

获取或设置列表控件中为 ListItem 所表示的项显示的文本。

Value

获取或设置与 ListItem 关联的值。

方法

Equals(Object)

确定指定的对象是否具有与当前列表项相同的值和文本。

FromString(String)

从指定的文本创建 ListItem

GetHashCode()

用作特定类型的哈希函数,适合在哈希算法和数据结构(如哈希表)中使用。

GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

显式接口实现

IAttributeAccessor.GetAttribute(String)

返回具有指定特性名称的列表项控件的特性值。

IAttributeAccessor.SetAttribute(String, String)

使用指定名称和值设置列表项控件的特性。

IParserAccessor.AddParsedSubObject(Object)

允许 Text 属性保持为内部内容。

IStateManager.IsTrackingViewState

有关此成员的说明,请参见 IsTrackingViewState

IStateManager.LoadViewState(Object)

有关此成员的说明,请参见 LoadViewState(Object)

IStateManager.SaveViewState()

有关此成员的说明,请参见 SaveViewState()

IStateManager.TrackViewState()

有关此成员的说明,请参见 TrackViewState()

适用于

另请参阅