Scott Mitchell's ASP. NET 2.0 data tutorial 45: DataList and Repeater data sorting (3)

Source: Internet
Author: User

Step 7:InRepeaterAdd sorting Function

Now that you have completed the custom page, we can add the sorting function. The GetProductsPagedAndSorted method of the ProductsBLL class is the same as that of GetProductsPaged.StartRowIndexAndMaximumRowsParameter. The difference is that it has anotherSortExpressionParameters. To use the GetProductsPagedAndSorted method in SortingWithCustomPaging. aspx, We need:

  1. Change the SelectMethod attribute of ObjectDataSource from GetProductsPaged to GetProductsPagedAndSorted.
  2. Add one for the SelectParameters parameter set of ObjectDataSourceSortExpressionParameter.
  3. Create a private attribute to store SortExpression through view state during the postback process.
  4. Modify the Selecting event handler of ObjectDataSourceSortExpressionThe parameter value is assigned to the SortExpression attribute (created in 3 ).
  5. Create a sorting page.

First, modify the SelectMethod attribute of ObjectDataSource and addSortExpressionParameters. OKSortExpressionThe type is String. After completing these steps, the declaration mark of ObjectDataSource should look similar to the following:

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:

Then add a SortExpression attribute with the value of view state. Use "ProductName" as the default value when no sort expression value is set.

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;

}

}

Before ObjectDataSource calls the GetProductsPagedAndSorted methodSortExpressionSet the parameter to the value of the SortExpression attribute. Add the following code in Selecting event handler:

C #

1

E. InputParameters ["sortExpression"] = SortExpression;

Now you only need to complete the sorting interface. Like in the previous example, we used three buttons to implement the sorting function, allowing users to sort by product name, category, and 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"/>

Create a Click event handler for all three buttons. Set StartRowIndex to 0, SortExpression to the corresponding value, and rebind the data to 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 ();

}

Now all work is done! The steps for customizing paging and sorting are similar to those for default paging. Figure 18 shows the last page of data sorted by category.


Figure
18:PressCategoryLast page of data sorted

Note: In the previous example, when sorting by supplier, the sorting expression is "SupplierName ". However, we need to use "CompanyName" when executing a custom page ". This is because the stored procedure of custom paging-GetProductsPagedAndSorted-passes the sort expression to ROW_NUMBER (). ROW_NUMBER () requires an actual column name instead of an alias. Therefore, we must use CompanyName (a column name in the Suppliers table) instead of SupplierName (alias in the SELECT statement) as expression.

Summary

Neither DataList nor Repeater provides built-in sorting support, but we can implement this function through a custom interface and a little bit of code. When only sorting is implemented (without paging), the sort expression can be passed to the Select method of ObjectDataSource through the cesceselectarguments object. The SortExpression attribute of the cesceselectarguments object can be assigned to the electing event handler of ObjectDataSource.

To add a sorting function for a DataList or Repeater with the sorting function, add a method to receive the sort expression in BLL. Then, this information can be passed through the SelectParameters parameter of ObjectDataSource.

This chapter completes the paging and sorting of DataList and Repeater. In the next chapter, that is, the last chapter, we will learn how to add a Button in the templates (Template) of DataList and Repeater to provide some custom item-based).

Happy programming!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.