Condividi tramite


ListViewSortEventArgs.SortExpression Proprietà

Definizione

Ottiene o imposta l'espressione utilizzata per ordinare gli elementi nel controllo 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

Valore della proprietà

Espressione utilizzata per ordinare gli elementi nel controllo ListView.

Esempio

Nell'esempio seguente viene illustrato come utilizzare l'oggetto ListViewSortEventArgs per visualizzare la direzione di ordinamento e la colonna in ordinamento. Questo esempio di codice fa parte di un esempio più ampio fornito per 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

Nell'esempio seguente viene illustrato come impostare la SortExpression proprietà per ordinare più colonne con istruzioni di ordinamento diverse applicate a ogni colonna.

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

Commenti

Quando viene generato l'evento Sorting , è possibile utilizzare la SortExpression proprietà per specificare l'espressione di ordinamento che verrà utilizzata per ordinare il ListView controllo. L'espressione di ordinamento contiene il nome del campo da ordinare. È anche possibile ordinare più colonne alla volta impostando questa proprietà a livello di codice su un elenco delimitato da virgole di nomi di campo.

Nota

Per impostazione predefinita, il ListView controllo applica la direzione di ordinamento all'elenco completo di colonne. Di conseguenza, la direzione di ordinamento si applica solo all'ultima colonna dell'elenco.

Si applica a

Vedi anche