This documentation is archived and is not being maintained.

ListViewSelectEventArgs Class

Provides data for the SelectedIndexChanging event.


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

public class ListViewSelectEventArgs : CancelEventArgs

The ListViewSelectEventArgs type exposes the following members.

Public methodListViewSelectEventArgsInitializes a new instance of the ListViewSelectEventArgs class.

Public propertyCancelGets or sets a value indicating whether the event should be canceled. (Inherited from CancelEventArgs.)
Public propertyNewSelectedIndexGets or sets the index of the new item to select in the ListView control.

Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

The ListView control raises the SelectedIndexChanging event when a Select button is clicked, but before the ListView control handles the select operation. (A Select a button is one whose CommandName property is set to "Select".) This enables you to provide an event-handling method that performs a custom routine whenever this event occurs, such as canceling the select operation.

A ListViewSelectEventArgs object is passed to the event-handling method, which enables you to determine the index of the item that is selected by the user. It also enables you to cancel the select operation. To cancel the select operation, set the Cancel property of the ListViewSelectEventArgs object to true.

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

The following example shows how to use the ListViewSelectEventArgs object that is passed to the SelectedIndexChanging event to cancel the select operation if the item that was selected is discontinued.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<script runat="server">

  void Page_Load()
    Message.Text = String.Empty;

  void ProductsListView_SelectedIndexChanging(Object sender, ListViewSelectEventArgs e)
    ListViewItem item = (ListViewItem)ProductsListView.Items[e.NewSelectedIndex];
    Label l = (Label)item.FindControl("DiscontinuedDateLabel");

    if (String.IsNullOrEmpty(l.Text))

    DateTime discontinued = DateTime.Parse(l.Text);
    if (discontinued < DateTime.Now)
      Message.Text = "You cannot select a discontinued item.";
      e.Cancel = true;

  protected void ProductsListView_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
    // Clear selection.
    ProductsListView.SelectedIndex = -1;

<html xmlns="" >
  <head id="Head1" runat="server">
    <title>ListView.SelectedIndexChanging Example</title>
    <form id="form1" runat="server">

      <h3>ListView.SelectedIndexChanging Example</h3>

      <asp:Label ID="Message"

      <asp:ListView ID="ProductsListView" 
        runat="server" >
          <table cellpadding="2" runat="server" id="tblProducts" width="640px">
            <tr runat="server" id="itemPlaceholder" />
          <asp:DataPager runat="server" ID="ProductsDataPager" PageSize="12">
                ShowFirstPageButton="true" ShowLastPageButton="true"
                FirstPageText="|&lt;&lt; " LastPageText=" &gt;&gt;|"
                NextPageText=" &gt; " PreviousPageText=" &lt; " />
          <tr runat="server">
            <td valign="top">
              <asp:LinkButton ID="SelectButton" runat="server" Text="..." CommandName="Select" />
            <td valign="top">
              <asp:Label ID="NameLabel" runat="server" Text='<%#Eval("Name") %>' />
            <td valign="top">
              <asp:Label ID="ProductNumberLabel" runat="server" Text='<%#Eval("ProductNumber") %>' />
              <asp:Label ID="DiscontinuedDateLabel" runat="server" Text='<%#Eval("DiscontinuedDate", "{0:d}") %>' />
          <tr runat="server" style="background-color:#ADD8E6">
            <td valign="top">
              <asp:Label ID="NameLabel" runat="server" Text='<%#Eval("Name") %>' />
            <td valign="top">
              <asp:Label ID="ProductNumberLabel" runat="server" Text='<%#Eval("ProductNumber") %>' />
              <asp:Label ID="DiscontinuedDateLabel" runat="server" Text='<%#Eval("DiscontinuedDate", "{0:d}") %>' />

      <asp:SqlDataSource ID="ProductsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ProductID], [Name], [ProductNumber], [DiscontinuedDate] 
          FROM Production.Product"
        UpdateCommand="UPDATE Production.Product
           SET Name = @Name, ProductNumber = @ProductNumber, DiscontinuedDate = @DiscontinuedDate
           WHERE ProductID = @ProductID">


.NET Framework

Supported in: 4, 3.5

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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