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 데이터 바인딩된 목록 컨트롤 내의 개별 데이터 항목(예: a 또는 컨트롤)을 ListBox RadioButtonList 나타냅니다.

목록 컨트롤의 항목에 대해 표시되는 텍스트를 지정하는 방법에는 여러 가지가 있습니다. 가장 일반적인 방법은 내부 HTML 콘텐츠에 텍스트를 배치하는 것입니다. 내부 HTML 콘텐츠는 컨트롤의 여는 태그와 닫는 태그 사이의 텍스트입니다 ListItem . 이 속성을 사용하여 항목의 Text 목록 컨트롤에 표시되는 텍스트를 지정할 수도 있습니다.

Value 속성을 사용하면 컨트롤에 표시된 텍스트 외에도 목록 컨트롤의 항목과 값을 연결할 수 있습니다. 예를 들어 목록 컨트롤 "Item 1"에서 항목에 대한 텍스트를 표시하고 속성을 사용하여 Value 해당 항목의 값을 지정할 수 있습니다(예: "$1.99".).

내부 HTML 콘텐츠 Text또는 Value 속성 집합의 조합을 사용할 수 있습니다. 컨트롤에 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>  

다음 표에서는 렌더링된 내부 HTML 콘텐츠 및 특성에 사용되는 set 속성과 해당 속성의 조합을 나열합니다 Value . 왼쪽의 세 열에는 set 속성의 조합이 나열됩니다. 오른쪽에 있는 두 열은 해당 특성에 사용되는 속성 값을 나열합니다.

내부 HTML 콘텐츠 Text 속성 Value 속성 렌더링된 내부 HTML 콘텐츠 렌더링된 값 특성
설정 설정 설정 내부 HTML 콘텐츠 Value 속성
설정 설정 설정 안 함 내부 HTML 콘텐츠 내부 HTML 콘텐츠
설정 설정 안 함 설정 내부 HTML 콘텐츠 Value 속성
설정 설정 안 함 설정 안 함 내부 HTML 콘텐츠 내부 HTML 텍스트
설정 안 함 설정 설정 Text 속성 Value 속성
설정 안 함 설정 설정 안 함 Text 속성 Text 속성
설정 안 함 설정 안 함 설정 Value 속성 Value 속성
설정 안 함 설정 안 함 설정 안 함 설정 안 함 설정 안 함

참고

Text 각 속성에는 Value 빈 문자열의 기본값이 있으므로 목록 컨트롤에 빈 목록 항목이 있을 수 있습니다.

목록 컨트롤이 표시되면 해당 Selected 속성이 설정된 true 모든 ListItem 컨트롤이 컨트롤에 강조 표시됩니다.

컨트롤은 ListItem 컨트롤을 Enabled 사용할지 여부를 ListItem 지정할 수 있는 속성을 제공합니다. ListItem 사용하지 않도록 설정된 컨트롤은 선택할 수 없음을 나타내기 위해 흐리게 표시됩니다. 컨트롤 또는 CheckBoxList 컨트롤에서 컨트롤을 ListItem RadioButtonList 사용 하지 않도록 설정 하려면이 속성을 사용 합니다.

참고

컨트롤 또는 ListBox 컨트롤에서 DropDownList 컨트롤을 ListItem 사용 하지 않도록 설정 하려면이 속성을 사용할 수 없습니다.

인스턴스의 초기 속성 값의 목록을 ListItem, 참조는 ListItem 생성자입니다.

주의

악성 클라이언트 스크립트 포함 될 수 있는 사용자 입력을 표시 하려면이 제어를 사용할 수 있습니다. 애플리케이션에서 표시 하기 전에 실행 스크립트, SQL 문 또는 다른 코드에 대 한 클라이언트에서 전송 되는 모든 정보를 확인 합니다. 입력된 텍스트 컨트롤에 표시 하기 전에 사용자 입력을 확인 하려면 유효성 검사 컨트롤을 사용할 수 있습니다. ASP.NET에서는 사용자 입력에서 차단 스크립트를 HTML 입력된 요청 유효성 검사 기능을 제공 합니다. 자세한 내용은 표준 컨트롤 보호, 방법: 보호에 대 한 스크립트 악용 문자열을 HTML 인코딩 적용 하 여 웹 애플리케이션에서, 및 에서 사용자 입력 유효성 검사 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()를 참조하세요.

적용 대상

추가 정보