This documentation is archived and is not being maintained.

ListView.Sort Method

Sorts the ListView control, depending on the specified sort expression and direction.

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

public virtual void Sort(
	string sortExpression,
	SortDirection sortDirection


Type: System.String
The sort expression to sort the ListView control with.
Type: System.Web.UI.WebControls.SortDirection
One of the SortDirection values.


The ListView control is bound to a data source control, but the DataSourceView object that is associated with the data source is null.

Use the Sort method to programmatically sort the data that is displayed in the ListView control by using the specified sort expression and direction. The sort expression specifies the column or columns to sort with. To sort multiple columns, create a sort expression that contains a comma-separated list of field names. The sort direction indicates whether sorting is performed in ascending or descending order.

You typically use this method when you want to sort the contents of the ListView control from outside the control, such as from a different control on the page. This method is also used to programmatically set a default sort order for the ListView control when it is first rendered.

This method raises the Sorted and Sorting events.

The following example shows how to use the Sort method to programmatically sort the ListView control by multiple data fields.

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

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

<script runat="server">        

  void SortButton_Click(Object sender, EventArgs e)

    // Create the sort expression from the values selected 
    // by the user from the DropDownList controls. Multiple
    // columns can be sorted by creating a sort expression
    // that contains a comma-separated list of field names.
    String expression = SortList1.SelectedValue + " " + 
        DirectionList1.SelectedValue + " ," + 

    // Determine the sort direction of the second column.
    // The sort direction parameter applies only to the 
    // last column sorted.
    SortDirection direction2 = SortDirection.Ascending;
    if (DirectionList2.SelectedValue == "DESC")
        direction2 = SortDirection.Descending;

    // Use the Sort method to programmatically sort the ListView
    // control using the sort expression and direction.
    ContactsListView.Sort(expression, direction2);



<html xmlns="" >
  <head id="Head1" runat="server"> 
    <title>ListView Sort Example</title>
    <style type="text/css">
      body {  font: 10pt Trebuchet MS; }
    <form id="form1" runat="server">

      <h3>ListView Sort Example</h3>

          <td>Sort by:</td>
            <asp:DropDownList ID="SortList1" runat="server">
              <asp:ListItem Selected="true">FirstName</asp:ListItem>
          <td>Sort order:</td>
            <asp:DropDownList ID="DirectionList1" runat="server">
              <asp:ListItem Value="ASC" Text="Ascending" Selected="True" />
              <asp:ListItem Value="DESC" Text="Descending" />
          <td>Then by:</td>
            <asp:DropDownList ID="SortList2" runat="server">
              <asp:ListItem Selected="true">LastName</asp:ListItem>
          <td>Sort order:</td>
            <asp:DropDownList ID="DirectionList2" runat="server">
              <asp:ListItem Value="ASC" Text="Ascending" Selected="True" />
              <asp:ListItem Value="DESC" Text="Descending" />

      <asp:Button id="SortButton"

      <br/><br />

      <asp:ListView ID="ContactsListView" 
          <table cellpadding="2" width="640px" border="1" runat="server" id="tblContacts">
            <tr runat="server">
              <th runat="server">ContactID</th>
              <th runat="server">FirstName</th>
              <th runat="server">LastName</th>
              <th runat="server">EmailAddress</th>
            <tr runat="server" id="itemPlaceholder" />
          <asp:DataPager runat="server" ID="ContactsDataPager" PageSize="12">
              <asp:NextPreviousPagerField ButtonType="Button"
                ShowLastPageButton="true" />
          <tr runat="server">
              <asp:Label ID="ContactIDLabel" runat="server" Text='<%#Eval("ContactID") %>' />
              <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:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ContactID], [FirstName], [LastName], [EmailAddress] 
          FROM Person.Contact">


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