ListView.Sorting 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在请求排序操作之后、ListView 控件处理排序操作之前发生。
public:
event EventHandler<System::Web::UI::WebControls::ListViewSortEventArgs ^> ^ Sorting;
public event EventHandler<System.Web.UI.WebControls.ListViewSortEventArgs> Sorting;
member this.Sorting : EventHandler<System.Web.UI.WebControls.ListViewSortEventArgs>
Public Custom Event Sorting As EventHandler(Of ListViewSortEventArgs)
事件类型
示例
以下示例演示如何为 Sorting 事件创建事件处理程序。
<%@ 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 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;
}
}
//</Snippet2>
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>ListView Sorting Example</title>
<style type="text/css">
body { font: 10pt Trebuchet MS, Arial, Tahoma; }
td { border: 1px solid #E6E6FA; }
.header { background: #B0C4DE; }
.alternatingItem { background: #edf5fd; }
</style>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView Sorting Example</h3>
<asp:ListView ID="ContactsListView"
DataSourceID="ContactsDataSource"
OnSorting="ContactsListView_Sorting"
runat="server">
<LayoutTemplate>
<table width="640px" runat="server" id="tblContacts">
<tr class="header" align="center" runat="server">
<td>
<asp:LinkButton runat="server" ID="SortByFirstNameButton"
CommandName="Sort" Text="First Name"
CommandArgument="FirstName"/>
<asp:Image runat="server" ID="SortImage1"
ImageUrl="~/images/ascending.gif" Visible="false" />
</td>
<td>
<asp:LinkButton runat="server" ID="SortByLastNameButton"
CommandName="Sort" Text="Last Name"
CommandArgument="LastName" />
<asp:Image runat="server" ID="SortImage2"
ImageUrl="~/images/ascending.gif" Visible="false" />
</td>
<td>
<asp:LinkButton runat="server" ID="SortByEmailButton"
CommandName="Sort" Text="Email Address"
CommandArgument="EmailAddress" />
<asp:Image runat="server" ID="SortImage3"
ImageUrl="~/images/ascending.gif" Visible="false" />
</td>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
<asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
<Fields>
<asp:NextPreviousPagerField
ShowFirstPageButton="true" ShowLastPageButton="true"
FirstPageText="|<< " LastPageText=" >>|"
NextPageText=" > " PreviousPageText=" < " />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<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="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr class="alternatingItem" runat="server">
<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="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
</td>
</tr>
</AlternatingItemTemplate>
</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 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
'</Snippet2>
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>ListView Sorting Example</title>
<style type="text/css">
body { font: 10pt Trebuchet MS, Arial, Tahoma; }
td { border: 1px solid #E6E6FA; }
.header { background: #B0C4DE; }
.alternatingItem { background: #edf5fd; }
</style>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView Sorting Example</h3>
<asp:ListView ID="ContactsListView"
DataSourceID="ContactsDataSource"
OnSorting="ContactsListView_Sorting"
runat="server">
<LayoutTemplate>
<table width="640px" runat="server" id="tblContacts">
<tr class="header" align="center" runat="server">
<td>
<asp:LinkButton runat="server" ID="SortByFirstNameButton"
CommandName="Sort" Text="First Name"
CommandArgument="FirstName"/>
<asp:Image runat="server" ID="SortImage1"
ImageUrl="~/images/ascending.gif" Visible="false" />
</td>
<td>
<asp:LinkButton runat="server" ID="SortByLastNameButton"
CommandName="Sort" Text="Last Name"
CommandArgument="LastName" />
<asp:Image runat="server" ID="SortImage2"
ImageUrl="~/images/ascending.gif" Visible="false" />
</td>
<td>
<asp:LinkButton runat="server" ID="SortByEmailButton"
CommandName="Sort" Text="Email Address"
CommandArgument="EmailAddress" />
<asp:Image runat="server" ID="SortImage3"
ImageUrl="~/images/ascending.gif" Visible="false" />
</td>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
<asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
<Fields>
<asp:NextPreviousPagerField
ShowFirstPageButton="true" ShowLastPageButton="true"
FirstPageText="|<< " LastPageText=" >>|"
NextPageText=" > " PreviousPageText=" < " />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<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="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr class="alternatingItem" runat="server">
<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="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
</td>
</tr>
</AlternatingItemTemplate>
</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 “排序”按钮或 Sort 调用 方法时,但在控件处理排序操作之前 ListView ,将引发 事件。 (“排序”按钮是一个按钮控件,其 CommandName 属性设置为“Sort”。) 这样,每当发生此事件时,就可以执行自定义例程,例如自定义排序表达式。
对象 ListViewSortEventArgs 将传递给事件处理程序,这使你可以确定列的排序表达式。 它还允许指示应取消排序操作。 若要取消排序操作,请将 Cancel 对象的 属性 ListViewSortEventArgs 设置为 true
。
有关如何处理事件的详细信息,请参阅 处理和引发事件。