ListViewSortEventArgs Class


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Provides data for the Sorting event.

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


public class ListViewSortEventArgs : CancelEventArgs

System_CAPS_pubmethodListViewSortEventArgs(String, SortDirection)

Initializes a new instance of the ListViewSortEventArgs class.


Gets or sets a value indicating whether the event should be canceled.(Inherited from CancelEventArgs.)


Gets or sets the direction in which to sort the ListView control.


Gets or sets the expression that is used to sort the items in the ListView control.


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.)

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

A ListViewSortEventArgs object is passed to the event-handling method. This object enables you to specify or determine the sort expression and sort direction for the ListView control. To determine the sort expression, use the SortExpression property. To determine the sort direction, use the SortDirection property. You can also cancel a sorting operation by setting the Cancel property to true.

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

The following example shows how to use the ListViewSortEventArgs object to display the sort direction and the column that is being sorted.

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

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

<script runat="server">
  void ContactsListView_Sorting(Object sender, ListViewSortEventArgs e)
    // Check the sort direction to set the image URL accordingly.
    string imgUrl;
    if (e.SortDirection == SortDirection.Ascending)
      imgUrl = "~/images/ascending.gif";
      imgUrl = "~/images/descending.gif";

    // Check which field is being sorted
    // to set the visibility of the image controls.
    Image sortImage1 = (Image) ContactsListView.FindControl("SortImage1");
    Image sortImage2 = (Image)ContactsListView.FindControl("SortImage2");
    Image sortImage3 = (Image)ContactsListView.FindControl("SortImage3");
    switch (e.SortExpression)
      case "FirstName":
        sortImage1.Visible = true;
        sortImage1.ImageUrl = imgUrl;
        sortImage2.Visible = false;
        sortImage3.Visible = false;
      case "LastName":
        sortImage1.Visible = false;
        sortImage2.Visible = true;
        sortImage2.ImageUrl = imgUrl;
        sortImage3.Visible = false;
      case "EmailAddress":
        sortImage1.Visible = false;
        sortImage2.Visible = false;
        sortImage3.Visible = true;
        sortImage3.ImageUrl = imgUrl;

<html xmlns="" >
  <head id="Head1" runat="server">
    <title>ListView Sorting Example</title>
    <style type="text/css">
      body  { font: 10pt Trebuchet MS, Arial, Tahoma; }
      td { border: 1px solid #E6E6FA; }
      .header {	background: #B0C4DE; }
      .alternatingItem { background: #edf5fd; }
    <form id="form1" runat="server">

      <h3>ListView Sorting Example</h3>

      <asp:ListView ID="ContactsListView" 
          <table width="640px" runat="server" id="tblContacts">
            <tr class="header" align="center" runat="server">
                <asp:LinkButton runat="server" ID="SortByFirstNameButton"
                  CommandName="Sort" Text="First Name" 
                <asp:Image runat="server" ID="SortImage1" 
                  ImageUrl="~/images/ascending.gif" Visible="false" />
                <asp:LinkButton runat="server" ID="SortByLastNameButton"
                  CommandName="Sort" Text="Last Name"
                  CommandArgument="LastName" />
                <asp:Image runat="server" ID="SortImage2" 
                  ImageUrl="~/images/ascending.gif" Visible="false" />
                <asp:LinkButton runat="server" ID="SortByEmailButton"
                  CommandName="Sort" Text="E-mail Address" 
                  CommandArgument="EmailAddress" />
                <asp:Image runat="server" ID="SortImage3" 
                  ImageUrl="~/images/ascending.gif" Visible="false" />
            <tr runat="server" id="itemPlaceholder" />
          <asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
                ShowFirstPageButton="true" ShowLastPageButton="true"
                FirstPageText="|&lt;&lt; " LastPageText=" &gt;&gt;|"
                NextPageText=" &gt; " PreviousPageText=" &lt; " />
          <tr runat="server">
              <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
              <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
              <asp:Label ID="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
          <tr class="alternatingItem" runat="server">
              <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
              <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
              <asp:Label ID="EmailLabel" 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:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ContactID], [FirstName], [LastName], [EmailAddress] 
          FROM Person.Contact" >


.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