ListViewItemEventArgs Klasa

Definicja

Udostępnia dane dla zdarzeń ItemCreated i ItemDataBound .

public ref class ListViewItemEventArgs : EventArgs
public class ListViewItemEventArgs : EventArgs
type ListViewItemEventArgs = class
    inherit EventArgs
Public Class ListViewItemEventArgs
Inherits EventArgs
Dziedziczenie
ListViewItemEventArgs

Przykłady

W poniższym przykładzie pokazano, jak używać ListViewItemEventArgs obiektu do uzyskiwania dostępu do właściwości elementu powiązanego z danymi.


<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    // <Snippet2>
    protected void ContactsListView_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        Label EmailAddressLabel;
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            // Display the email address in italics.
            EmailAddressLabel = (Label)e.Item.FindControl("EmailAddressLabel");
            EmailAddressLabel.Font.Italic = true;

            System.Data.DataRowView rowView = e.Item.DataItem as System.Data.DataRowView;
            string currentEmailAddress = rowView["EmailAddress"].ToString();
            if (currentEmailAddress == "orlando0@adventure-works.com")
            {
                EmailAddressLabel.Font.Bold = true;
            }
        }
    }
    // </Snippet2>
  
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>ListView ItemDataBound Example</title>
</head>
<body style="font: 10pt Trebuchet MS">
    <form id="form1" runat="server">
    <h3>
        ListView ItemDataBound Example</h3>
    <asp:ListView ID="ContactsListView" DataSourceID="ContactsDataSource" ConvertEmptyStringToNull="true"
        OnItemDataBound="ContactsListView_ItemDataBound" runat="server">
        <LayoutTemplate>
            <table cellpadding="2" width="680px" border="0">
                <tr style="background-color: #ADD8E6" runat="server">
                    <th runat="server">
                        First Name
                    </th>
                    <th runat="server">
                        Last Name
                    </th>
                    <th runat="server">
                        Email Address
                    </th>
                </tr>
                <tr runat="server" id="itemPlaceholder" />
            </table>
            <asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
                <Fields>
                    <asp:NumericPagerField ButtonCount="10" />
                </Fields>
            </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
            <tr style="background-color: #CAEEFF" runat="server">
                <td>
                    <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
                </td>
                <td>
                    <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
                </td>
                <td>
                    <asp:Label ID="EmailAddressLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
                </td>
            </tr>
        </ItemTemplate>
    </asp:ListView>
    <!-- This example uses Microsoft SQL Server and connects      -->
    <!-- to the AdventureWorks sample database. Use an ASP.NET    -->
    <!-- expression to retrieve the connection string value       -->
    <!-- from the Web.config file.                                -->
    <asp:SqlDataSource ID="ContactsDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>"
        SelectCommand="SELECT FirstName, LastName, EmailAddress FROM SalesLT.Customer">
    </asp:SqlDataSource>
    </form>
</body>
</html>

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    ' <Snippet2>
    Protected Sub ContactsListView_ItemDataBound(ByVal sender As Object, _
                                                 ByVal e As ListViewItemEventArgs)
  
        If e.Item.ItemType = ListViewItemType.DataItem Then
            ' Display the email address in italics.
            Dim EmailAddressLabel As Label = _
              CType(e.Item.FindControl("EmailAddressLabel"), Label)
            EmailAddressLabel.Font.Italic = True

            Dim rowView As System.Data.DataRowView
            rowView = CType(e.Item.DataItem, System.Data.DataRowView)
            Dim currentEmailAddress As String = rowView("EmailAddress").ToString()
            If currentEmailAddress = "orlando0@adventure-works.com" Then
                EmailAddressLabel.Font.Bold = True
            End If
        End If
        
    End Sub
    ' </Snippet2>

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>ListView ItemDataBound Example</title>
</head>
<body style="font: 10pt Trebuchet MS">
    <form id="form1" runat="server">
    <h3>
        ListView ItemDataBound Example</h3>
    <asp:ListView ID="ContactsListView" DataSourceID="ContactsDataSource" ConvertEmptyStringToNull="true"
        OnItemDataBound="ContactsListView_ItemDataBound" runat="server">
        <layouttemplate>
          <table cellpadding="2" width="680px" border="0">
            <tr style="background-color: #ADD8E6" runat="server">
                <th runat="server">First Name</th>
                <th runat="server">Last Name</th>
                <th runat="server">Email Address</th>
            </tr>
            <tr runat="server" id="itemPlaceholder" />
          </table>
          <asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
            <Fields>
              <asp:NumericPagerField ButtonCount="10" /> 
            </Fields>
          </asp:DataPager>
        </layouttemplate>
        <itemtemplate>
          <tr style="background-color: #CAEEFF" runat="server">
            <td>
              <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
            </td>
            <td>
              <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
            </td>
            <td>
              <asp:Label ID="EmailAddressLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
            </td>
          </tr>
        </itemtemplate>
    </asp:ListView>
    <!-- This example uses Microsoft SQL Server and connects      -->
    <!-- to the AdventureWorks sample database. Use an ASP.NET    -->
    <!-- expression to retrieve the connection string value       -->
    <!-- from the Web.config file.                                -->
    <asp:SqlDataSource ID="ContactsDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>"
        SelectCommand="SELECT FirstName, LastName, EmailAddress FROM SalesLT.Customer">
    </asp:SqlDataSource>
    </form>
</body>
</html>

Uwagi

Przed renderowaniem ListViewItem kontrolki ListView należy utworzyć obiekt dla każdego elementu w kontrolce. Zdarzenie ItemCreated jest wywoływane za każdym razem, gdy element w kontrolce ListView zostanie utworzony. Dzięki temu można podać metodę obsługi zdarzeń, która wykonuje niestandardową procedurę za każdym razem, gdy element zostanie utworzony, na przykład dodanie niestandardowej zawartości do elementu.

Podobnie przed renderowaniem kontrolki ListView każdy element w kontrolce musi być powiązany z rekordem w źródle danych. Zdarzenie ItemDataBound jest zgłaszane, gdy element (reprezentowany przez ListViewItem obiekt) jest powiązany z danymi w kontrolce ListView . Dzięki temu można wykonać procedurę niestandardową za każdym razem, gdy element jest powiązany z danymi, takimi jak modyfikowanie wartości danych przed ich wyświetleniem.

ListViewItemEventArgs Obiekt jest przekazywany do metody obsługi zdarzeń, która umożliwia dostęp do właściwości elementu, który wzbudził zdarzenie. Aby określić, który typ elementu (element danych, pusty element lub wstaw element) jest tworzony, użyj ItemType właściwości ListViewItem obiektu.

Aby uzyskać listę początkowych wartości właściwości dla wystąpienia programu ListViewItemEventArgs, zobacz ListViewItemEventArgs konstruktor.

Konstruktory

ListViewItemEventArgs(ListViewItem)

Inicjuje nowe wystąpienie klasy ListViewItemEventArgs.

Właściwości

Item

Pobiera element, który jest tworzony lub powiązany z danymi.

Metody

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera bieżące wystąpienie.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Dotyczy

Zobacz też