在ASP.NET 2.0中操作資料之四十四:DataList和Repeater資料排序(三)_自學過程

來源:互聯網
上載者:User

第七步: 在自訂分頁的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: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>

然後添加一個SortExpression屬性,它的值為view state。在沒有設任何sort expression的值時候,使用“ProductName”作為預設值。

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裡添加以下代碼:

e.InputParameters["sortExpression"] = SortExpression;

  現在只需要完成排序介面就可以了。和我們上一個例子一樣,我們使用3個button來實現排序功能,允許使用者根據product name, category, supplier來排序。

<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。

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參數傳進去。

  祝編程快樂!

作者簡介

  本系列教程作者 Scott Mitchell,著有六本ASP/ASP.NET方面的書,是4GuysFromRolla.com的創始人,自1998年以來一直應用 微軟Web技術。大家可以點擊查看全部教程《[翻譯]Scott Mitchell 的ASP.NET 2.0資料教程》,希望對大家的學習ASP.NET有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.