Scott Mitchell 的ASP.NET 2.0資料教程之四十五::DataList和Repeater資料排序(三)

來源:互聯網
上載者:User

第七步: 在自訂分頁的Repeater 裡添加排序功能

現在已經完成了自訂分頁,我們再來添加排序功能。ProductsBLL類的GetProductsPagedAndSorted方法和GetProductsPaged一樣有startRowIndexmaximumRows 參數,不一樣的是它還多了一個sortExpression 參數。在SortingWithCustomPaging.aspx裡使用GetProductsPagedAndSorted方法我們需要:

  1. 將ObjectDataSource的SelectMethod屬性從GetProductsPaged改為GetProductsPagedAndSorted。
  2. 為ObjectDataSource的SelectParameters參數集合增加一個sortExpression Parameter。
  3. 建立一個私人的屬性用來在postback過程中通過view state儲存SortExpression。
  4. 修改ObjectDataSource的Selecting event handler將ObjectDataSource的sortExpression 參數值賦為SortExpression 屬性(3中建立的)。
  5. 建立排序介面。

首先修改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(項)的功能。

祝編程愉快!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.