GridViewSortEventArgs GridViewSortEventArgs GridViewSortEventArgs GridViewSortEventArgs Class

Definizione

Fornisce dati per l'evento Sorting.Provides data for the Sorting event.

public ref class GridViewSortEventArgs : System::ComponentModel::CancelEventArgs
public class GridViewSortEventArgs : System.ComponentModel.CancelEventArgs
type GridViewSortEventArgs = class
    inherit CancelEventArgs
Public Class GridViewSortEventArgs
Inherits CancelEventArgs
Ereditarietà
GridViewSortEventArgsGridViewSortEventArgsGridViewSortEventArgsGridViewSortEventArgs

Esempi

Nell'esempio seguente viene illustrato come utilizzare l' GridViewSortEventArgs oggetto passato al metodo di gestione degli eventi per annullare l'operazione di ordinamento quando l'utente tenta di ordinare la colonna Address in un GridView controllo.The following example demonstrates how to use the GridViewSortEventArgs object passed to the event-handling method to cancel the sorting operation when the user attempts to sort the address column in a GridView control.


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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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 xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView Sorting Example</title>
</head>
<body>
    <form id="form1" 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>


<%@ Page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  
  Sub CustomersGridView_Sorting(sender As Object, e As GridViewSortEventArgs)
  
    ' Cancel the sorting operation if the user attempts
    ' to sort by address.
    If e.SortExpression = "Address" Then
    
      e.Cancel = True
      Message.Text = "You cannot sort by address."
      SortInformationLabel.Text = ""
    
    Else
    
      Message.Text = ""
      
    End If
    
  End Sub

  Sub CustomersGridView_Sorted(ByVal sender As Object, ByVal e As EventArgs)
 
    ' Display the sort expression and sort direction.
    SortInformationLabel.Text = "Sorting by " & _
      CustomersGridView.SortExpression.ToString() & _
      " in " & CustomersGridView.SortDirection.ToString() & _
      " order."
    
  End Sub
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView Sorted and Sorting Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView Sorted and 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>

Commenti

Il GridView controllo genera l' Sorting evento quando viene fatto clic su un pulsante di ordinamento CommandName (un pulsante con la relativa proprietà impostata su "Sort"), GridView ma prima che il controllo gestisca l'operazione di ordinamento.The GridView control raises the Sorting event when a Sort button (a button with its CommandName property set to "Sort") is clicked, but before the GridView control handles the sort operation. Questo consente di fornire un metodo di gestione degli eventi che esegue una routine personalizzata, ad esempio l'annullamento dell'operazione di ordinamento, ogni volta che si verifica questo evento.This allows you to provide an event-handling method that performs a custom routine, such as canceling the sorting operation, whenever this event occurs.

Nota

Anche se qualsiasi pulsante all' GridView interno di un CommandName controllo con la relativa proprietà impostata su "Sort Sorting " genera l'evento, i pulsanti di ordinamento vengono in genere GridView visualizzati nella riga di intestazione di un controllo.Although any button within a GridView control with its CommandName property set to "Sort" will raise the Sorting event, Sort buttons typically appear in the header row of a GridView control.

Un GridViewSortEventArgs oggetto viene passato al metodo di gestione degli eventi, che consente di specificare o determinare l'espressione di ordinamento e la direzione di ordinamento in cui ordinare GridView il controllo.A GridViewSortEventArgs object is passed to the event-handling method, which allows you to specify or determine the sort expression and sort direction in which to sort the GridView control. Per determinare l'espressione di ordinamento, utilizzare SortExpression la proprietà.To determine the sort expression, use the SortExpression property. Per determinare la direzione di ordinamento, utilizzare SortDirection la proprietà.To determine the sort direction, use the SortDirection property. È anche possibile annullare un'operazione di ordinamento impostando la Cancel proprietà su true.You can also cancel a sorting operation by setting the Cancel property to true.

Per altre informazioni su come gestire gli eventi, vedere la gestione e generazione di eventi.For more information about how to handle events, see Handling and Raising Events.

Per un elenco dei valori iniziali delle proprietà di un'istanza GridViewSortEventArgsdi, vedere GridViewSelectEventArgs il costruttore.For a list of initial property values for an instance of GridViewSortEventArgs, see the GridViewSelectEventArgs constructor.

Costruttori

GridViewSortEventArgs(String, SortDirection) GridViewSortEventArgs(String, SortDirection) GridViewSortEventArgs(String, SortDirection) GridViewSortEventArgs(String, SortDirection)

Inizializza una nuova istanza della classe GridViewSortEventArgs.Initializes a new instance of the GridViewSortEventArgs class.

Proprietà

Cancel Cancel Cancel Cancel

Ottiene o imposta un valore che indica se l'evento debba essere annullato.Gets or sets a value indicating whether the event should be canceled.

(Inherited from CancelEventArgs)
SortDirection SortDirection SortDirection SortDirection

Ottiene o imposta la direzione in cui ordinare il controllo GridView.Gets or sets the direction in which to sort the GridView control.

SortExpression SortExpression SortExpression SortExpression

Ottiene o imposta l'espressione utilizzata per ordinare gli elementi nel controllo GridView.Gets or sets the expression used to sort the items in the GridView control.

Metodi

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Funge da funzione hash predefinita.Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Ottiene l'oggetto Type dell'istanza corrente.Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.Returns a string that represents the current object.

(Inherited from Object)

Si applica a

Vedi anche