ListViewSortEventArgs.SortExpression 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置用于对 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
属性值
用于对 ListView 控件中的项进行排序的表达式。
示例
以下示例演示如何使用 ListViewSortEventArgs 对象显示排序方向和正在排序的列。 此代码示例是为 ListViewSortEventArgs 类提供的一个更大示例的一部分。
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
以下示例演示如何设置 SortExpression 属性,以便对多列进行排序,并为每个列应用了不同的排序方向。
<%@ 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>
注解
Sorting引发事件时,可以使用 SortExpression 属性指定将用于对控件进行排序的ListView排序表达式。 排序表达式包含要排序的字段的名称。 还可以通过以编程方式将此属性设置为以逗号分隔的字段名称列表,一次对多个列进行排序。
注意
默认情况下,控件 ListView 将排序方向应用于列的完整列表。 因此,排序方向仅适用于列表中的最后一列。