ListViewItemEventArgs Class
Collapse the table of content
Expand the table of content

ListViewItemEventArgs Class


Provides data for the ItemCreated and ItemDataBound events.

Namespace:   System.Web.UI.WebControls
Assembly:  System.Web.Extensions (in System.Web.Extensions.dll)


public class ListViewItemEventArgs : EventArgs


Initializes a new instance of the ListViewItemEventArgs class.


Gets the item that is being created or bound to data.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Creates a shallow copy of the current Object.(Inherited from Object.)


Returns a string that represents the current object.(Inherited from Object.)

Before the ListView control can be rendered, a ListViewItem object must be created for each item in the control. The ItemCreated event is raised every time that an item in the ListView control is created. This enables you to provide an event-handling method that performs a custom routine whenever an item is created, such as adding custom content to an item.

Similarly, before the ListView control can be rendered, every item in the control must be bound to a record in the data source. The ItemDataBound event is raised when an item (represented by a ListViewItem object) is bound to data in the ListView control. This enables you to perform a custom routine whenever an item is bound to data, such as modifying the values of the data before they are displayed.

A ListViewItemEventArgs object is passed to the event-handling method, which enables you to access the properties of the item that raised the event. To determine which item type (data item, empty item, or insert item) is being created, use the ItemType property of the ListViewItem object.

For a list of initial property values for an instance of ListViewItemEventArgs, see the ListViewItemEventArgs constructor.

The following example shows how to use the ListViewItemEventArgs object to access the properties of the item that is being bound to data.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">

    // <Snippet2>
    protected void ContactsListView_ItemDataBound(object sender, ListViewItemEventArgs e)
        Label EmailAddressLabel;
        if (e.Item.ItemType == ListViewItemType.DataItem)
            // Display the e-mail 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 == "")
                EmailAddressLabel.Font.Bold = true;
    // </Snippet2>

<html xmlns="">
<head id="Head1" runat="server">
    <title>ListView ItemDataBound Example</title>
<body style="font: 10pt Trebuchet MS">
    <form id="form1" runat="server">
        ListView ItemDataBound Example</h3>
    <asp:ListView ID="ContactsListView" DataSourceID="ContactsDataSource" ConvertEmptyStringToNull="true"
        OnItemDataBound="ContactsListView_ItemDataBound" runat="server">
            <table cellpadding="2" width="680px" border="0">
                <tr style="background-color: #ADD8E6" runat="server">
                    <th runat="server">
                        First Name
                    <th runat="server">
                        Last Name
                    <th runat="server">
                        E-mail Address
                <tr runat="server" id="itemPlaceholder" />
            <asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
                    <asp:NumericPagerField ButtonCount="10" />
            <tr style="background-color: #CAEEFF" runat="server">
                    <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
                    <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
                    <asp:Label ID="EmailAddressLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
    <!-- 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">

.NET Framework
Available since 3.5

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
© 2015 Microsoft