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
属性
実装

次の例は、コントロール内でのコントロールの ListItem 使用を ListBox 示しています。

Note

次のコード サンプルでは、単一ファイル コード モデルを使用します。分離コード ファイルに直接コピーすると、正しく動作しない可能性があります。 各コード サンプルは、拡張子が .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 、 コントロールや コントロールなどの ListBox データ バインド リスト コントロール内の個々のデータ項目を RadioButtonList 表します。

リスト コントロール内の項目に表示されるテキストを指定するには、いくつかの方法があります。 最も一般的な方法は、内部 HTML コンテンツにテキストを配置することです。 内部 HTML コンテンツは、コントロールの開始タグと終了タグの間の ListItem テキストです。 プロパティを Text 使用して、アイテムのリスト コントロールに表示されるテキストを指定することもできます。

Valueプロパティを使用すると、コントロールに表示されるテキストに加えて、リスト コントロール内の項目に値を関連付けることができます。 たとえば、 などの "Item 1"リスト コントロール内のアイテムのテキストを表示し、 プロパティを Value 使用してそのアイテムの値 (など "$1.99") を指定できます。

内部 HTML コンテンツ、、 Text、または Value プロパティセットを任意に組み合わせて使用できます。 コントロールの結果の HTML 出力は、 ListItem 設定されているこれら 3 つのプロパティの組み合わせによって異なります。 たとえば、3 つのプロパティすべてが次のように設定されている場合は、次のようになります。

<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 属性に使用される対応するプロパティの組み合わせを示します。 左側の 3 つの列には、セット プロパティの組み合わせが一覧表示されます。 右側の 2 つの列には、対応する属性に使用されるプロパティ値が表示されます。

内部 HTML コンテンツ Text プロパティ Value プロパティ レンダリングされた内部 HTML コンテンツ Rendered Value 属性
オン オン オン 内部 HTML コンテンツ Value プロパティ
オン オン 未設定 内部 HTML コンテンツ 内部 HTML コンテンツ
オン 未設定 オン 内部 HTML コンテンツ Value プロパティ
オン 未設定 未設定 内部 HTML コンテンツ 内部 HTML テキスト
未設定 オン オン Text プロパティ Value プロパティ
未設定 オン 未設定 Text プロパティ Text プロパティ
未設定 未設定 オン Value プロパティ Value プロパティ
未設定 未設定 未設定 未設定 未設定

Note

Textプロパティと Value プロパティにはそれぞれ空の文字列の既定値があるため、リスト コントロールに空のリスト アイテムを含めできます。

リスト コントロールが表示されると、そのSelectedプロパティが にtrue設定されているコントロールListItemがコントロールで強調表示されます。

コントロールには ListItemEnabled プロパティが用意されており、コントロールが有効か無効かを ListItem 指定できます。 ListItem無効になっているコントロールは、選択できないことを示すために淡色表示されます。 コントロールまたはコントロールのコントロールを ListItem 無効にするには、 RadioButtonList このプロパティを CheckBoxList 使用します。

Note

このプロパティを使用して、コントロールまたはListBoxコントロールのコントロールをDropDownList無効ListItemにすることはできません。

のインスタンスの初期プロパティ値の ListItem一覧については、 コンストラクターを ListItem 参照してください。

注意事項

このコントロールを使用して、悪意のあるクライアント スクリプトを含む可能性があるユーザー入力を表示できます。 クライアントから送信された情報で、実行可能スクリプト、SQL ステートメント、またはその他のコードを確認してから、アプリケーションに表示します。 入力コントロールを使用して、コントロールに入力テキストを表示する前にユーザー入力を確認できます。 ASP.NET は、ユーザー入力のスクリプトと HTML をブロックする入力要求検証機能を提供します。 詳細については、「標準コントロールのセキュリティ保護」、「方法: 文字列に HTML エンコードを適用して Web アプリケーションのスクリプトの悪用から保護する」、および「ASP.NET Web ページでのユーザー入力の検証」を参照してください。

コンストラクター

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()」をご覧ください。

適用対象

こちらもご覧ください