第七步: 在自訂分頁的Repeater 裡添加排序功能
現在已經完成了自訂分頁,我們再來添加排序功能。ProductsBLL類的GetProductsPagedAndSorted方法和GetProductsPaged一樣有startRowIndex 和 maximumRows 參數,不一樣的是它還多了一個sortExpression 參數。在SortingWithCustomPaging.aspx裡使用GetProductsPagedAndSorted方法我們需要:
- 將ObjectDataSource的SelectMethod屬性從GetProductsPaged改為GetProductsPagedAndSorted。
- 為ObjectDataSource的SelectParameters參數集合增加一個sortExpression Parameter。
- 建立一個私人的屬性用來在postback過程中通過view state儲存SortExpression。
- 修改ObjectDataSource的Selecting event handler將ObjectDataSource的sortExpression 參數值賦為SortExpression 屬性(3中建立的)。
- 建立排序介面。
首先修改ObjectDataSource的SelectMethod屬性並添加sortExpression 參數。確定sortExpression 的類型是String。完成這些後ObjectDataSource的聲明標記看起來應該和下面差不多:
ASP.NET |
1 2 3 4 5 6 7 8 9 10 |
<asp:ObjectDataSource ID="ProductsDataSource" runat="server" OldValuesParameterFormatString="original_{0}" TypeName="ProductsBLL" SelectMethod="GetProductsPagedAndSorted" OnSelecting="ProductsDataSource_Selecting"> <SelectParameters> <asp:Parameter Name="sortExpression" Type="String" /> <asp:Parameter Name="startRowIndex" Type="Int32" /> <asp:Parameter Name="maximumRows" Type="Int32" /> </SelectParameters> </asp:ObjectDataSource> |
Next, we need a page-level SortExpression property whose value is serialized to view state. If no sort expression value has been set, use “ProductName” as the default:
然後添加一個SortExpression屬性,它的值為view state。在沒有設任何sort expression的值時候,使用“ProductName”作為預設值。
C# |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
private string SortExpression { get { object o = ViewState["SortExpression"]; if (o == null) return "ProductName"; else return o.ToString(); } set { ViewState["SortExpression"] = value; } } |
在ObjectDataSource調用GetProductsPagedAndSorted方法前,我們需要將sortExpression 參數設為SortExpression屬性的值。在Selecting event handler裡添加以下代碼:
C# |
1 |
e.InputParameters["sortExpression"] = SortExpression; |
現在只需要完成排序介面就可以了。和我們上一個例子一樣,我們使用3個button來實現排序功能,允許使用者根據product name, category, supplier來排序。
ASP.NET |
1 2 3 4 5 6 |
<asp:Button runat="server" id="SortByProductName" Text="Sort by Product Name" /> <asp:Button runat="server" id="SortByCategoryName" Text="Sort by Category" /> <asp:Button runat="server" id="SortBySupplierName" Text="Sort by Supplier" /> |
為這三個button都建立Click event handler。在其中將StartRowIndex設為0,SortExpression設為相應的值,並將資料重新綁定到Repeater。
C# |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
protected void SortByProductName_Click(object sender, EventArgs e) { StartRowIndex = 0; SortExpression = "ProductName"; Products.DataBind(); } protected void SortByCategoryName_Click(object sender, EventArgs e) { StartRowIndex = 0; SortExpression = "CategoryName"; Products.DataBind(); } protected void SortBySupplierName_Click(object sender, EventArgs e) { StartRowIndex = 0; SortExpression = "CompanyName"; Products.DataBind(); } |
現在所有工作都完成了!實現自訂分頁和排序的一些步驟和預設分頁差不多。圖18顯示的當按照category排序時的最後一頁資料。
圖 18: 按Category排序的最後一頁資料
注意:在前面的例子裡,當按照supplier排序時排序運算式為” SupplierName”。然而執行自訂分頁時我們需要使用” CompanyName”。這是因為自訂分頁的預存程序–GetProductsPagedAndSorted–將sort expression傳給ROW_NUMBER(),ROW_NUMBER()需要一個實際的列名,而不是別名。因此我們必須使用CompanyName(Suppliers表的一個列名),而不是使用SupplierName (SELECT語句裡的別名)作為expression。
總結
無論是DataList還是Repeater都沒有提供內建的排序支援,但是通過自訂介面和一點點代碼,我們可以實現這樣的功能。當僅僅只實現排序時(不包含分頁),sort expression可以通過DataSourceSelectArguments對象傳給ObjectDataSource的Select方法。DataSourceSelectArguments對象的SortExpression屬性可以在ObjectDataSource的electing event handler裡賦值。
為已經有排序功能的DataList或Repeater添加排序功能,最簡單的方法是在BLL裡添加一個接收sort expression的方法。然後這個資訊可以通過ObjectDataSource的SelectParameters參數傳進去。
本章完成了DataList和Repeater的分頁和排序。下一章,也就是最後一章,我們將學習如何在DataList和Repeater的templates(模板)裡添加Button,用來提供一些自訂的基於但個item(項)的功能。
祝編程愉快!