第七步: 在自定义分页的Repeater 里添加排序功能

现在已经完成了自定义分页,我们再来添加排序功能。ProductsBLL类的GetProductsPagedAndSorted方法和GetProductsPaged一样有startRowIndex maximumRows 参数,不一样的是它还多了一个sortExpression 参数。在SortingWithCustomPaging.aspx里使用GetProductsPagedAndSorted方法我们需要:

  1. ObjectDataSourceSelectMethod属性从GetProductsPaged改为GetProductsPagedAndSorted
  2. ObjectDataSourceSelectParameters参数集合增加一个sortExpression Parameter
  3. 创建一个私有的属性用来在postback过程中通过view state存储SortExpression
  4. 修改ObjectDataSourceSelecting event handlerObjectDataSourcesortExpression 参数值赋为SortExpression 属性(3中创建的)。
  5. 创建排序界面。

首先修改ObjectDataSourceSelectMethod属性并添加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;

现在只需要完成排序界面就可以了。和我们上一个例子一样,我们使用3button来实现排序功能,允许用户根据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设为0SortExpression设为相应的值,并将数据重新绑定到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”。这是因为自定义分页的存储过程GetProductsPagedAndSortedsort expression传给ROW_NUMBER()ROW_NUMBER()需要一个实际的列名,而不是别名。因此我们必须使用CompanyNameSuppliers表的一个列名),而不是使用SupplierName (SELECT语句里的别名)作为expression

总结

无论是DataList还是Repeater都没有提供内置的排序支持,但是通过自定义界面和一点点代码,我们可以实现这样的功能。当仅仅只实现排序时(不包含分页),sort expression可以通过DataSourceSelectArguments对象传给ObjectDataSourceSelect方法。DataSourceSelectArguments对象的SortExpression属性可以在ObjectDataSourceelecting event handler里赋值。

为已经有排序功能的DataListRepeater添加排序功能,最简单的方法是在BLL里添加一个接收sort expression的方法。然后这个信息可以通过ObjectDataSourceSelectParameters参数传进去。

本章完成了DataListRepeater的分页和排序。下一章,也就是最后一章,我们将学习如何在DataListRepeatertemplates(模板)里添加Button,用来提供一些自定义的基于但个item(项)的功能。

祝编程愉快!

posted on 2006-12-14 17:41  有些伤感  阅读(2867)  评论(2编辑  收藏  举报