ListViewItemEventArgs Classe

Definizione

Fornisce i dati per gli eventi ItemCreated e ItemDataBound.

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

Esempio

Nell'esempio seguente viene illustrato come utilizzare l'oggetto ListViewItemEventArgs per accedere alle proprietà dell'elemento associato ai dati.


<%@ 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>

Commenti

Prima di poter eseguire il rendering del ListView controllo, è necessario creare un ListViewItem oggetto per ogni elemento nel controllo . L'evento ItemCreated viene generato ogni volta che viene creato un elemento nel ListView controllo . In questo modo è possibile fornire un metodo di gestione degli eventi che esegue una routine personalizzata ogni volta che viene creato un elemento, ad esempio l'aggiunta di contenuto personalizzato a un elemento.

Analogamente, prima di poter eseguire il rendering del ListView controllo, ogni elemento del controllo deve essere associato a un record nell'origine dati. L'evento ItemDataBound viene generato quando un elemento (rappresentato da un ListViewItem oggetto) è associato ai dati nel ListView controllo . In questo modo è possibile eseguire una routine personalizzata ogni volta che un elemento è associato ai dati, ad esempio modificando i valori dei dati prima che vengano visualizzati.

Un ListViewItemEventArgs oggetto viene passato al metodo di gestione degli eventi, che consente di accedere alle proprietà dell'elemento che ha generato l'evento. Per determinare quale tipo di elemento (elemento dati, elemento vuoto o elemento di inserimento) viene creato, utilizzare la ItemType proprietà dell'oggetto ListViewItem .

Per un elenco dei valori iniziali delle proprietà per un'istanza di ListViewItemEventArgs, vedere il ListViewItemEventArgs costruttore .

Costruttori

ListViewItemEventArgs(ListViewItem)

Inizializza una nuova istanza della classe ListViewItemEventArgs.

Proprietà

Item

Ottiene l'elemento da creare o da associare ai dati.

Metodi

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Si applica a

Vedi anche