Partager via


ListViewSortEventArgs.SortExpression Propriété

Définition

Obtient ou définit l'expression utilisée pour trier les éléments du contrôle ListView.

public:
 property System::String ^ SortExpression { System::String ^ get(); void set(System::String ^ value); };
public string SortExpression { get; set; }
member this.SortExpression : string with get, set
Public Property SortExpression As String

Valeur de propriété

Expression utilisée pour trier les éléments du contrôle ListView.

Exemples

L’exemple suivant montre comment utiliser l’objet ListViewSortEventArgs pour afficher le sens de tri et la colonne triée. Cet exemple de code fait partie d’un exemple plus grand fourni pour la ListViewSortEventArgs classe .

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";
  else
    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;
      break;
    case "LastName":
      sortImage1.Visible = false;
      sortImage2.Visible = true;
      sortImage2.ImageUrl = imgUrl;
      sortImage3.Visible = false;
      break;
    case "EmailAddress":
      sortImage1.Visible = false;
      sortImage2.Visible = false;
      sortImage3.Visible = true;
      sortImage3.ImageUrl = imgUrl;
      break;
  }
}
Sub ContactsListView_Sorting(ByVal sender As Object, ByVal e As ListViewSortEventArgs) 

  ' Check the sort direction to set the image URL accordingly.
  Dim imgUrl As String
  If e.SortDirection = SortDirection.Ascending Then
    imgUrl = "~/images/ascending.gif"
  Else
    imgUrl = "~/images/descending.gif"
  End If
  
  ' Check which field is being sorted
  ' to set the visibility of the image controls.
  Dim sortImage1 As Image = CType(ContactsListView.FindControl("SortImage1"), Image)
  Dim sortImage2 As Image = CType(ContactsListView.FindControl("SortImage2"), Image)
  Dim sortImage3 As Image = CType(ContactsListView.FindControl("SortImage3"), Image)
  
  Select Case 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
  End Select
  
End Sub

L’exemple suivant montre comment définir la SortExpression propriété afin de trier plusieurs colonnes avec différentes directions de tri appliquées à chaque colonne.

<%@ 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">        

  protected void ContactsListView_Sorting(object sender, ListViewSortEventArgs 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
    // and optionally directions for each column.
    e.SortExpression = SortList1.SelectedValue + " " +
        DirectionList1.SelectedValue + " ," +
        SortList2.SelectedValue;

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

  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server"> 
    <title>SortExpression Example</title>
    <style type="text/css">
      body {  font: 10pt Trebuchet MS; }
    </style>
  </head>
  <body>
    <form id="form1" runat="server">
          
      <h3>SortExpression Example</h3>

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

      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        runat="server" onsorting="ContactsListView_Sorting">
        <LayoutTemplate>
          <asp:Button id="SortButton"
            Text="Sort"
            CommandName="Sort"
            runat="server"/>  

          <br/><br />

          <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>
            <tr runat="server" id="itemPlaceholder" />
          </table>
          <asp:DataPager runat="server" ID="ContactsDataPager" PageSize="12">
            <Fields>
              <asp:NextPreviousPagerField ButtonType="Button"
                ShowFirstPageButton="true" 
                ShowLastPageButton="true" />
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td>
              <asp:Label ID="ContactIDLabel" runat="server" Text='<%#Eval("ContactID") %>' />
            </td>
            <td>
              <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
            </td>
            <td>
              <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
            </td>
            <td>
              <asp:Label ID="EmailAddressLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
            </td>
          </tr>
        </ItemTemplate>
      </asp:ListView>              
        
      <!-- 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">
      </asp:SqlDataSource>
        
    </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 ContactsListView_Sorting(ByVal sender As Object, ByVal e As ListViewSortEventArgs)
    
    ' 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
    ' and optionally directions for each column.
    e.SortExpression = SortList1.SelectedValue & " " & _
      DirectionList1.SelectedValue & " ," & SortList2.SelectedValue
    
    ' Determine the sort direction of the second column.
    ' The sort direction parameter applies only to the 
    ' last column sorted.
    e.SortDirection = SortDirection.Ascending
    If DirectionList2.SelectedValue = "DESC" Then
      e.SortDirection = SortDirection.Descending
    End If
    
  End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server"> 
    <title>SortExpression Example</title>
    <style type="text/css">
      body {  font: 10pt Trebuchet MS; }
    </style>
  </head>
  <body>
    <form id="form1" runat="server">
          
      <h3>SortExpression Example</h3>

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

      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        runat="server" onsorting="ContactsListView_Sorting">
        <LayoutTemplate>
          <asp:Button id="SortButton"
            Text="Sort"
            CommandName="Sort"
            runat="server"/>  

          <br/><br />

          <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>
            <tr runat="server" id="itemPlaceholder" />
          </table>
          <asp:DataPager runat="server" ID="ContactsDataPager" PageSize="12">
            <Fields>
              <asp:NextPreviousPagerField ButtonType="Button"
                ShowFirstPageButton="true" 
                ShowLastPageButton="true" />
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td>
              <asp:Label ID="ContactIDLabel" runat="server" Text='<%#Eval("ContactID") %>' />
            </td>
            <td>
              <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
            </td>
            <td>
              <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
            </td>
            <td>
              <asp:Label ID="EmailAddressLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
            </td>
          </tr>
        </ItemTemplate>
      </asp:ListView>              
        
      <!-- 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">
      </asp:SqlDataSource>
        
    </form>
  </body>
</html>

Remarques

Lorsque l’événement Sorting est déclenché, vous pouvez utiliser la SortExpression propriété pour spécifier l’expression de tri qui sera utilisée pour trier le ListView contrôle. L’expression de tri contient le nom du champ à trier. Vous pouvez également trier plusieurs colonnes à la fois en définissant par programmation cette propriété sur une liste de noms de champs séparés par des virgules.

Notes

Par défaut, le ListView contrôle applique le sens de tri à la liste complète des colonnes. Par conséquent, le sens de tri s’applique uniquement à la dernière colonne de la liste.

S’applique à

Voir aussi