DataListItemCollection 클래스

정의

DataListItem 컨트롤에 있는 DataList 개체의 컬렉션을 나타냅니다. 이 클래스는 상속될 수 없습니다.

public ref class DataListItemCollection sealed : System::Collections::ICollection
public sealed class DataListItemCollection : System.Collections.ICollection
type DataListItemCollection = class
    interface ICollection
    interface IEnumerable
Public NotInheritable Class DataListItemCollection
Implements ICollection
상속
DataListItemCollection
구현

예제

다음 예제에서는 형식으로 데이터 원본을 만듭니다는 DataView 컨트롤과에 바인딩합니다를 DataList 컨트롤 태그에 선언 합니다. 그런 다음 기본 내용의 표시 DataListItemCollection 페이지의 개체입니다.

<%@ Import Namespace = "System.Data"  %>
<%@ 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">
<script runat="server">    
// The following example creates a DataSource as a DataView.
// The DataView is bound to a DataList that is displayed using an
// ItemTemplate. When the page is first loaded, the program uses the 
// CopyTo method to copy the entire data source to an Array and then 
// displays that data.

ICollection CreateDataSource()
{
    DataTable myDataTable = new DataTable();
    DataRow myDataRow;

    myDataTable.Columns.Add(new DataColumn("EmployeeName", typeof(string)));
    myDataTable.Columns.Add(new DataColumn("EmployeeID", typeof(long)));
    for (int i = 0; i < 3; i++)
    {
        myDataRow = myDataTable.NewRow();
        myDataRow[0] = "somename" + i.ToString();
        myDataRow[1] = (i+1000);
        myDataTable.Rows.Add(myDataRow);
    }
    DataView dataView = new DataView(myDataTable);
    return dataView;
}

// <Snippet2>
void Page_Load(Object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // Bind the DataView to the DataSource.
        myDataList.DataSource = CreateDataSource();
        myDataList.DataBind();
        // Create a Array to hold the DataSource.
        System.Array myArray = Array.CreateInstance(typeof(DataListItem),
            myDataList.Items.Count);
        // Copy the DataSource to an Array.
        myDataList.Items.CopyTo(myArray,0);
        PrintValues(myArray);
    }
}

// Prints each element in the Array onto the label lblAllItems1.
public void PrintValues(Array myArr)
{
    DataListItem currentItem;
    System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
    while (myEnumerator.MoveNext())
    {
        currentItem = (DataListItem)myEnumerator.Current;
        lblAllItems1.Text += "<br /><br />" + 
            ((Label)(currentItem.Controls[1])).Text;
    }
}

// Event handler method for show button.
void show_Click(Object sender,EventArgs e)
{
    // Get the underlying DataListItemCollection from the DataList object.
    DataListItemCollection myDataListItemCollection = myDataList.Items;
    // Display the read-only properties.
    Response.Write("<b>The Total number of items are " + 
        myDataListItemCollection.Count + "</b>");
    Response.Write("<br /><b>The ReadOnly property of the " + 
        "DataListItemCollection is always" + 
        myDataListItemCollection.IsReadOnly + "</b>");
    Response.Write("<br /><b>The IsSynchronized property of the " +
        "DataListItemCollection is always " +
        myDataListItemCollection.IsSynchronized + "</b>");
    myDataListItemCollection = null;
}
// </Snippet2>

// <Snippet5>
void allItems_Click(Object sender,EventArgs e)
{
    IEnumerator dataListEnumerator;
    DataListItem currentItem;
    lblAllItems.Text = "";
    // Get an enumerator to traverse the DataListItemCollection.
    dataListEnumerator = myDataList.Items.GetEnumerator();
    while(dataListEnumerator.MoveNext())
    {
        currentItem = (DataListItem)dataListEnumerator.Current;
        // Display the current DataListItem onto the label.
        lblAllItems.Text += ((Label)(currentItem.Controls[1])).Text + " ";
    }
}
// </Snippet5>

// <Snippet6>
void itemSelected(Object sender,EventArgs e)
{
    // Get the underlying DataListItemCollection from the DataList object.
    DataListItemCollection myDataListItemCollection = myDataList.Items;
    // Get the index of the selected radio button in the RadioButtonList.
    int index = Convert.ToInt16(listItemNo.SelectedItem.Value);
    // Get the DataListItem corresponding to index from DataList.
    // SyncRoot is used to make access to the DataListItemCollection 
    // in a thread-safe manner It returns the object that invoked it. 
    DataListItem currentItem = 
        ((DataListItemCollection)(myDataListItemCollection.SyncRoot))[index];
    // Display the selected DataListItem onto a label.
    lblDisplay.Text = "<b>DataListItem" + index + " is : "
        + ((Label)(currentItem.Controls[1])).Text;
    currentItem = null;
    myDataListItemCollection = null;
}
// </Snippet6>

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>
        DataListItemCollection Example
    </title>
</head>
<body>
<form runat="server" id="Form1">
    <h3>
        DataListItemCollection Example
    </h3>
    <table>
        <tr>
            <td>
                <asp:datalist id="myDataList" runat="server" Font-Size="8pt" 
                    Font-Names="Verdana" BorderColor="black" CellSpacing="5"
                    CellPadding="10" GridLines="Horizontal">
                    <HeaderStyle BackColor="#aaaadd"></HeaderStyle>
                    <HeaderTemplate>
                        EmployeeName EmployeeID
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label id="label1" runat="server"
                            Text='<%# DataBinder.Eval(Container.DataItem, "EmployeeName") %>'>
                        </asp:Label>
                            
                        <%# DataBinder.Eval(Container.DataItem, "EmployeeID")%>
                    </ItemTemplate>
                </asp:datalist>
            </td>
            <td>
                <asp:label id="lblAllItems1" 
                    Text="The following items <br /> are copied to the array: " 
                    Runat="server" ForeColor="blue" Font-Bold="true" 
                    AssociatedControlID="listItemNo">
                    The following items <br /> are copied to the array:
                </asp:label>
            </td>
            <td>
                    <b>Show Items:</b>
                <asp:RadioButtonList ID="listItemNo" 
                    OnSelectedIndexChanged="itemSelected" AutoPostBack="true" 
                    Runat="server">
                    <asp:ListItem Value="0" Text="0"></asp:ListItem>
                    <asp:ListItem Value="1" Text="1"></asp:ListItem>
                    <asp:ListItem Value="2" Text="2"></asp:ListItem>
                </asp:RadioButtonList>
            </td>
            <td>
                <asp:Label ID="lblDisplay" Runat="server" />
            </td>
        </tr>
    </table>
    <p>
        <asp:button id="show" onclick="show_Click" Runat="server" 
             Font-Bold="True" Text="DataList Information" />
        <asp:button id="allitems" onclick="allItems_Click" Runat="server" 
            Font-Bold="True" Text="Show All DataListItems" />
    </p>
    <p>
        <b>All DataList items will be shown here:</b>
        <asp:label id="lblAllItems" Runat="server" ForeColor="blue" />
    </p>
</form>
</body>
</html>
<%@ Import Namespace = "System.Data"  %>
<%@ 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">
<script runat="server">    
' The following example creates a DataSource as a DataView.
' The DataView is bound to a DataList that is displayed using an
' ItemTemplate. When the page is first loaded, the program uses the 
' CopyTo method to copy the entire data source to an Array and then 
' displays that data.

Function CreateDataSource() As ICollection 
    Dim myDataTable As DataTable = New DataTable()
    Dim myDataRow As DataRow 
    Dim i As Integer

    myDataTable.Columns.Add(New DataColumn("EmployeeName", GetType(String)))
    myDataTable.Columns.Add(New DataColumn("EmployeeID", GetType(Integer)))
    For i = 0 To 2
        myDataRow = myDataTable.NewRow()
        myDataRow(0) = "somename" + i.ToString()
        myDataRow(1) = i + 1000
        myDataTable.Rows.Add(myDataRow)
    Next 
    CreateDataSource = new DataView(myDataTable)
End Function

' <Snippet2>
Sub Page_Load(sender As object, e As EventArgs)
    If (Not IsPostBack)
        ' Bind the DataView to the DataSource.
        myDataList.DataSource = CreateDataSource()
        myDataList.DataBind()
        ' Create a Array to hold the DataSource.
        Dim myArray As System.Array = Array.CreateInstance(GetType(DataListItem), _
            myDataList.Items.Count)
        ' Copy the DataSource to an Array.
        myDataList.Items.CopyTo(myArray, 0)
        PrintValues(myArray)
    End If
End sub

' Prints each element in the Array onto the label lblAllItems1.
Public Sub PrintValues(myArr As Array)
    Dim currentItem As DataListItem 
    Dim myEnumerator As System.Collections.IEnumerator = myArr.GetEnumerator()
    While (myEnumerator.MoveNext())
        currentItem = CType(myEnumerator.Current,DataListItem)
        lblAllItems1.Text = lblAllItems1.Text & "<br /><br />" & _
            CType(currentItem.Controls(1),Label).Text
    End While
End Sub

    ' Event handler method for show button.
Sub show_Click(sender as Object, e As EventArgs)
    ' Get the underlying DataListItemCollection from the DataList object.
    Dim myDataListItemCollection As DataListItemCollection = myDataList.Items
    ' Display the read-only properties.
    Response.Write("<b>The Total number of items are " & _
        myDataListItemCollection.Count & "</b>")
        Response.Write("<br /><b>The ReadOnly property of the " & _
            "DataListItemCollection is always " & _
            myDataListItemCollection.IsReadOnly & "</b>")
        Response.Write("<br /><b>The IsSynchronized property of the " & _
            "DataListItemCollection is always " _
            & myDataListItemCollection.IsSynchronized & "</b>")
    myDataListItemCollection = Nothing
End Sub
    ' </Snippet2>

' <Snippet5>
Sub AllItems_Click(sender As Object, e As EventArgs)
    Dim dataListEnumerator As IEnumerator
    Dim currentItem As DataListItem 
    lblAllItems.Text = ""
    ' Get an enumerator to traverse the DataListItemCollection.
    dataListEnumerator = myDataList.Items.GetEnumerator()
    while(dataListEnumerator.MoveNext())
        currentItem = CType(dataListEnumerator.Current,DataListItem)
        ' Display the current DataListItem onto the label.
        lblAllItems.Text = lblAllItems.Text & CType((currentItem.Controls(1)), _
        Label).Text & "  "
    End While
End Sub
' </Snippet5>

' <Snippet6>
Sub ItemSelected(sender As object, e As EventArgs)
    ' Get the underlying DataListItemCollection from the DataList object.
    Dim myDataListItemCollection As DataListItemCollection  = myDataList.Items
    ' Get the index of the selected radio button in the RadioButtonList.
    Dim index As Integer = Convert.ToInt16(listItemNo.SelectedItem.Value)
    ' Get the DataListItem corresponding to index from DataList.
    ' SyncRoot is used to make access to the DataListItemCollection 
    ' in a thread-safe manner It returns the object that invoked it. 
    Dim currentItem As DataListItem = _ 
        CType(myDataListItemCollection.SyncRoot,DataListItemCollection)(index)
    ' Display the selected DataListItem onto a label.
    lblDisplay.Text = "<b>DataListItem" & index & " is: " _
        & CType(currentItem.Controls(1),Label).Text
    currentItem = Nothing
    myDataListItemCollection = Nothing
End Sub
    ' </Snippet6>

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>
        DataListItemCollection Example
    </title>
</head>
<body>
<form runat="server" id="Form1">
    <h3>
        DataListItemCollection Example
    </h3>
    <table>
        <tr>
            <td>
                <asp:datalist id="myDataList" runat="server" Font-Size="8pt" 
                    Font-Names="Verdana" BorderColor="black" CellSpacing="5"
                    CellPadding="10" GridLines="Horizontal">
                    <HeaderStyle BackColor="#aaaadd"></HeaderStyle>
                    <HeaderTemplate>
                        EmployeeName EmployeeID
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label id="label1"  runat="server"
                            Text='<%# DataBinder.Eval(Container.DataItem, "EmployeeName") %>' />
                            
                        <%# DataBinder.Eval(Container.DataItem, "EmployeeID")%>
                    </ItemTemplate>
                </asp:datalist>
            </td>
            <td>
                <asp:label id="lblAllItems1" 
                    Text="The following items <br /> are copied to the array: " 
                    Runat="server" ForeColor="blue" Font-Bold="true" 
                    AssociatedControlID="listItemNo">
                    The following items <br /> are copied to the array:
                </asp:label>
            </td>
            <td>
                    <b>Show Items:</b>
                <asp:RadioButtonList ID="listItemNo" 
                    OnSelectedIndexChanged="itemSelected" AutoPostBack="true" 
                    Runat="server">
                    <asp:ListItem Value="0" Text="0"></asp:ListItem>
                    <asp:ListItem Value="1" Text="1"></asp:ListItem>
                    <asp:ListItem Value="2" Text="2"></asp:ListItem>
                </asp:RadioButtonList>
            </td>
            <td>
                <asp:Label ID="lblDisplay" Runat="server" />
            </td>
        </tr>
    </table>
    <p>
        <asp:button id="show" onclick="show_Click" Runat="server" 
            Font-Bold="True" Text="DataList Information" />
        <asp:button id="allitems" onclick="allItems_Click" Runat="server" 
            Font-Bold="True" Text="Show All DataListItems" />
    </p>
    <p>
        <b>All DataList items will be shown here:</b>
        <asp:label id="lblAllItems" Runat="server" ForeColor="blue" />
    </p>
</form>
</body>
</html>

설명

합니다 DataListItemCollection 클래스의 컬렉션을 나타냅니다 DataListItem 개체를 나타내는 데이터 항목에는 DataList 제어 합니다. 프로그래밍 방식으로 검색할 DataListItem 에서 개체를 DataList 제어 다음 방법 중 하나를 사용 합니다.

  • 인덱서를 사용 하 여 단일 가져옵니다 DataListItem 배열 표기법을 사용 하 여 컬렉션에서 개체입니다.

  • 사용 하 여는 CopyTo 컬렉션의 내용을 복사 하는 메서드를 System.Array 컬렉션에서 항목을 가져오려면 다음 사용할 수 있는 개체입니다.

  • 사용 합니다 GetEnumerator 메서드를를 System.Collections.IEnumerator 컬렉션에서 항목을 가져오려면 다음 사용할 수 있는 인터페이스입니다.

Count 속성 컬렉션에서 항목의 총 수를 지정 하 고는 일반적으로 컬렉션의 상한을 결정 하는 데 사용 됩니다.

생성자

DataListItemCollection(ArrayList)

DataListItemCollection 클래스의 새 인스턴스를 초기화합니다.

속성

Count

컬렉션에 있는 DataListItem 개체의 수를 가져옵니다.

IsReadOnly

DataListItem에 있는 DataListItemCollection 개체를 수정할 수 있는지 여부를 나타내는 값을 가져옵니다.

IsSynchronized

DataListItemCollection에 대한 액세스가 동기화되어 스레드로부터 안전하게 보호되는지 여부를 나타내는 값을 가져옵니다.

Item[Int32]

컬렉션의 지정된 인덱스에 해당하는 DataListItem 개체를 가져옵니다.

SyncRoot

DataListItemCollection 컬렉션에 대한 액세스를 동기화하는 데 사용할 수 있는 개체를 가져옵니다.

메서드

CopyTo(Array, Int32)

DataListItemCollection 개체의 지정된 인덱스에서 시작하여 이 Array 컬렉션에서 지정된 Array 개체로 모든 항목을 복사합니다.

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetEnumerator()

IEnumerator에 있는 DataListItem 개체를 모두 포함하는DataListItemCollection 인터페이스를 반환합니다.

GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

확장 메서드

Cast<TResult>(IEnumerable)

IEnumerable의 요소를 지정된 형식으로 캐스팅합니다.

OfType<TResult>(IEnumerable)

지정된 형식에 따라 IEnumerable의 요소를 필터링합니다.

AsParallel(IEnumerable)

쿼리를 병렬화할 수 있도록 합니다.

AsQueryable(IEnumerable)

IEnumerableIQueryable로 변환합니다.

적용 대상

추가 정보