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 Form程式碼模型的詳細資訊,請參閱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 內容 轉譯的 Value 屬性
設定 設定 設定 內部 HTML 內容 Value 屬性
設定 設定 未設定 內部 HTML 內容 內部 HTML 內容
設定 未設定 設定 內部 HTML 內容 Value 屬性
設定 未設定 未設定 內部 HTML 內容 內部 HTML 文字
未設定 設定 設定 Text 屬性 Value 屬性
未設定 設定 未設定 Text 屬性 Text 屬性
未設定 未設定 設定 Value 屬性 Value 屬性
未設定 未設定 未設定 未設定 未設定

注意

Text由於 和 Value 屬性各有空字串的預設值,因此可以在清單控制項中擁有空的清單專案。

顯示清單控制項時,控制項中已將其 Selected 屬性設定為 true 反白顯示的任何 ListItem 控制項。

控制項 ListItem 提供 Enabled 屬性,可讓您指定控制項是否 ListItem 已啟用或停用。 ListItem停用的控制項會呈現暗灰色,表示無法選取它。 使用這個屬性可停用 ListItem 控制項或 CheckBoxList 控制項中的 RadioButtonList 控制項。

注意

您無法使用這個屬性來停用 ListItem 控制項或 ListBox 控制項中的 DropDownList 控制項。

如需 實例 ListItem 的初始屬性值清單,請參閱 建 ListItem 構函式。

警告

此控制項可用來顯示使用者輸入,其中可能包含惡意用戶端腳本。 在應用程式中顯示可執行檔腳本、SQL 語句或其他程式碼之前,請先檢查從用戶端傳送的任何資訊。 您可以在控制項中顯示輸入文字之前,先使用驗證控制項來驗證使用者輸入。 ASP.NET 提供輸入要求驗證功能,以封鎖使用者輸入中的腳本和 HTML。 如需詳細資訊,請參閱保護標準控制項如何:將 HTML 編碼套用至字串來保護 Web 應用程式中的腳本惡意探索,以及在ASP.NET Web Pages中驗證使用者輸入

建構函式

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()

適用於

另請參閱