ListView.Sort(String, SortDirection) 메서드

정의

지정된 정렬 식과 방향에 따라 ListView 컨트롤을 정렬합니다.

public:
 virtual void Sort(System::String ^ sortExpression, System::Web::UI::WebControls::SortDirection sortDirection);
public virtual void Sort (string sortExpression, System.Web.UI.WebControls.SortDirection sortDirection);
abstract member Sort : string * System.Web.UI.WebControls.SortDirection -> unit
override this.Sort : string * System.Web.UI.WebControls.SortDirection -> unit
Public Overridable Sub Sort (sortExpression As String, sortDirection As SortDirection)

매개 변수

sortExpression
String

ListView 컨트롤을 정렬하는 데 사용할 정렬 식입니다.

sortDirection
SortDirection

SortDirection 값 중 하나입니다.

예외

ListView 컨트롤이 데이터 소스 컨트롤에 바인딩되어 있지만 데이터 소스와 연결된 DataSourceView 개체가 null인 경우

예제

다음 예제에서는 메서드를 사용하여 Sort 여러 데이터 필드별로 컨트롤을 프로그래밍 방식으로 정렬하는 ListView 방법을 보여 줍니다.

<%@ 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">        
    
  // <Snippet2>
  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 + " ," + 
        SortList2.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);

  }
  // </Snippet2>
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server"> 
    <title>ListView Sort Example</title>
    <style type="text/css">
      body {  font: 10pt Trebuchet MS; }
    </style>
  </head>
  <body>
    <form id="form1" runat="server">
          
      <h3>ListView Sort 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:Button id="SortButton"
        Text="Sort"
        OnClick="SortButton_Click" 
        runat="server"/>  

      <br/><br />

      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        runat="server">
        <LayoutTemplate>
          <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">        
    
  ' <Snippet2>
  Sub SortButton_Click(ByVal sender As Object, ByVal e As EventArgs) 
      
    ' 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.
    Dim expression As String = 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.
    Dim direction2 As SortDirection = SortDirection.Ascending
    If DirectionList2.SelectedValue = "DESC" Then
        direction2 = SortDirection.Descending
    End If
    
    ' Use the Sort method to programmatically sort the ListView
    ' control using the sort expression and direction.
    ContactsListView.Sort(expression, direction2)

  End Sub
  ' </Snippet2>
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server"> 
    <title>ListView Sort Example</title>
    <style type="text/css">
      body {  font: 10pt Trebuchet MS; }
    </style>
  </head>
  <body>
    <form id="form1" runat="server">
          
      <h3>ListView Sort 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:Button id="SortButton"
        Text="Sort"
        OnClick="SortButton_Click" 
        runat="server"/>  

      <br/><br />

      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        runat="server">
        <LayoutTemplate>
          <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>

설명

메서드를 Sort 사용하여 지정된 정렬 식 및 방향을 사용하여 컨트롤에 ListView 표시되는 데이터를 프로그래밍 방식으로 정렬합니다. 정렬 식은 정렬할 열 또는 열을 지정합니다. 여러 열을 정렬하려면 필드 이름의 쉼표로 구분된 목록을 포함하는 정렬 식을 만듭니다. 정렬 방향은 정렬이 오름차순 또는 내림차순으로 수행되는지 여부를 나타냅니다.

일반적으로 페이지의 다른 컨트롤과 같이 컨트롤의 내용을 ListView 컨트롤 외부에서 정렬하려는 경우 이 메서드를 사용합니다. 이 메서드는 처음 렌더링될 때 컨트롤에 대한 기본 정렬 순서를 ListView 프로그래밍 방식으로 설정하는 데도 사용됩니다.

이 메서드는 및 Sorting 이벤트를 발생합니다Sorted.

적용 대상

추가 정보