Export (0) Print
Expand All
4 out of 31 rated this helpful - Rate this topic

GridView.Sorting Event

Note: This event is new in the .NET Framework version 2.0.

Occurs when the hyperlink to sort a column is clicked, but before the GridView control handles the sort operation.

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

public event GridViewSortEventHandler Sorting
/** @event */
public void add_Sorting (GridViewSortEventHandler value)

/** @event */
public void remove_Sorting (GridViewSortEventHandler value)

JScript supports the use of events, but not the declaration of new ones.

The Sorting event is raised when the hyperlink to sort a column is clicked, but before the GridView control handles the sort operation. This allows you to provide an event-handling method that performs a custom routine, such as canceling the sorting operation, whenever this event occurs.

A GridViewSortEventArgs object is passed to the event-handling method, which allows you to determine the sort expression for the column and to indicate that the selection operation should be canceled. To cancel the selection operation, set the Cancel property of the GridViewSortEventArgs object to true.

For more information about handling events, see Consuming Events.

The following code example demonstrates how to use the Sorting event to cancel a sorting operation.


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

<script runat="server">
  
  void CustomersGridView_Sorting(Object sender, GridViewSortEventArgs e)
  {
    // Cancel the sorting operation if the user attempts
    // to sort by address.
    if (e.SortExpression == "Address")
    {
      e.Cancel = true;
      Message.Text = "You cannot sort by address.";
      SortInformationLabel.Text = "";
    }
    else
    {
      Message.Text = "";
    }
  }

  void CustomersGridView_Sorted(Object sender, EventArgs e)
  {
    // Display the sort expression and sort direction.
    SortInformationLabel.Text = "Sorting by " +
      CustomersGridView.SortExpression.ToString() +
      " in " + CustomersGridView.SortDirection.ToString() +
      " order.";
  }
  
</script>

<html>
  <body>
    <form runat="server">
        
      <h3>GridView Sorting Example</h3>

      <asp:label id="Message"
        forecolor="Red"
        runat="server"/>
        
      <br/>
        
      <asp:label id="SortInformationLabel"
        forecolor="Navy"
        runat="server"/>
                
      <br/>  

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSource" 
        autogeneratecolumns="true"
        allowpaging="true"
        emptydatatext="No data available." 
        allowsorting="true"
        onsorting="CustomersGridView_Sorting"
        onsorted="CustomersGridView_Sorted"  
        runat="server">
                
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSource"
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
        
    </form>
  </body>
</html>


Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.