本文部分摘自《ASP.NET4权威指南》
前面我们讲过,QueryExtender 控件支持多种可用于筛选数据的选项。但在使用筛选器选项之后,你还可以使用 OrderByExpression 对象来排序数据。其中,OrderByExpression 类可以按指定列和排序方向对数据进行排序
在使用OrderByExpression时,你可以使用它的DataField 属性指定要排序的数据字段;使用 Direction 属性指定排序方向。OrderByExpression 对象应用到数据源后,你还可以使用 ThenBy 表达式对另一个数据字段执行后续排序。
下面的示例程序演示了如何在ASP.NET4数据库的Employee数据表的Department列中,搜索员工部门等于SearchTextBox文本框中指定的字符串开头的员工信息。其中,OrderByExpression 对象按Workdate数据字段的降序和 EmployeeID字段的升序对数据排序。最后从LinqDataSource 控件返回的结果显示在 GridView 控件中。如代码清单10-6所示:
代码清单10-6:OrderByExpressionTest.aspx
代码
<form id="form1" runat="server">
搜索员工部门:
<asp:TextBox ID="SearchTextBox" runat="server" />
<asp:Button ID="Button1" runat="server" Text="搜索" />
<br />
<br />
<asp:LinqDataSource ID="LinqDataSource1" TableName="Employees"
runat="server" ContextTypeName="_10_2.EmployeesDataContext"
EntityTypeName="" Select="new (employeeid, employeename,
department, address, email, workdate)">
</asp:LinqDataSource>
<asp:QueryExtender ID="QueryExtender1" runat="server"
TargetControlID="LinqDataSource1">
<asp:SearchExpression SearchType="StartsWith"
DataFields="department">
<asp:ControlParameter ControlID="SearchTextBox" />
</asp:SearchExpression>
<asp:OrderByExpression DataField="workdate"
Direction="Descending">
<asp:ThenBy DataField="employeeid"
Direction="Ascending" />
</asp:OrderByExpression>
</asp:QueryExtender>
<asp:GridView ID="GridView1" runat="server" Width="100%"
DataSourceID="LinqDataSource1"
AllowPaging="True" AutoGenerateColumns="False"
DataKeyNames="employeeid">
<Columns>
<asp:BoundField DataField="employeeid" HeaderText="编号"
ReadOnly="True" SortExpression="employeeid" />
<asp:BoundField DataField="employeename" HeaderText="姓名"
SortExpression="employeename" />
<asp:BoundField DataField="department" HeaderText="部门"
SortExpression="department" />
<asp:BoundField DataField="address" HeaderText="住址"
SortExpression="address" />
<asp:BoundField DataField="email" HeaderText="邮箱"
SortExpression="email" />
<asp:BoundField DataField="workdate" HeaderText="工作时间"
SortExpression="workdate" />
</Columns>
</asp:GridView>
</form>
搜索员工部门:
<asp:TextBox ID="SearchTextBox" runat="server" />
<asp:Button ID="Button1" runat="server" Text="搜索" />
<br />
<br />
<asp:LinqDataSource ID="LinqDataSource1" TableName="Employees"
runat="server" ContextTypeName="_10_2.EmployeesDataContext"
EntityTypeName="" Select="new (employeeid, employeename,
department, address, email, workdate)">
</asp:LinqDataSource>
<asp:QueryExtender ID="QueryExtender1" runat="server"
TargetControlID="LinqDataSource1">
<asp:SearchExpression SearchType="StartsWith"
DataFields="department">
<asp:ControlParameter ControlID="SearchTextBox" />
</asp:SearchExpression>
<asp:OrderByExpression DataField="workdate"
Direction="Descending">
<asp:ThenBy DataField="employeeid"
Direction="Ascending" />
</asp:OrderByExpression>
</asp:QueryExtender>
<asp:GridView ID="GridView1" runat="server" Width="100%"
DataSourceID="LinqDataSource1"
AllowPaging="True" AutoGenerateColumns="False"
DataKeyNames="employeeid">
<Columns>
<asp:BoundField DataField="employeeid" HeaderText="编号"
ReadOnly="True" SortExpression="employeeid" />
<asp:BoundField DataField="employeename" HeaderText="姓名"
SortExpression="employeename" />
<asp:BoundField DataField="department" HeaderText="部门"
SortExpression="department" />
<asp:BoundField DataField="address" HeaderText="住址"
SortExpression="address" />
<asp:BoundField DataField="email" HeaderText="邮箱"
SortExpression="email" />
<asp:BoundField DataField="workdate" HeaderText="工作时间"
SortExpression="workdate" />
</Columns>
</asp:GridView>
</form>
示例运行结果图10-25所示:
图10-25:示例运行结果
本文部分摘自《ASP.NET4权威指南》