Manipulating data in ASP.net 2.0 44: DataList and Repeater data Sorting (iii) _ self-study process

Source: Internet
Author: User

Step Seventh: Add sorting functionality to the custom pagination repeater

Now that you have finished customizing the paging, let's add the sorting function. The getproductspagedandsorted method of the PRODUCTSBLL class has the same startrowindex and maximumrows parameters as getproductspaged. The difference is that it also has one more sortexpression parameter. Using the Getproductspagedandsorted method in sortingwithcustompaging.aspx we need to:

Change the ObjectDataSource SelectMethod property from Getproductspaged to getproductspagedandsorted.
Adds a sortexpression Parameter to the SelectParameters parameter collection for ObjectDataSource.
Creates a private property that is used to store sortexpression through the view state during the postback process.
Modify the selecting event handler of ObjectDataSource to assign the SortExpression parameter value of ObjectDataSource to the SortExpression property (created in 3).
Create a sort interface.

First modify the ObjectDataSource SelectMethod property and add the SortExpression parameter. Determines the type of the SortExpression is a string. After completing these, the ObjectDataSource declaration tags should look similar to the following:

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

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

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 method, we need to set the SortExpression parameter to the value of the SortExpression property. Add the following code to the selecting event handler:

e.inputparameters["SortExpression"] = SortExpression;

Now you just have to finish the sort interface. As in our previous example, we use 3 button to implement the sorting function, allowing the user to sort according to product name, category, supplier.

<asp:button runat= "Server" id= "Sortbyproductname"
 text= "Sort by Product Name"/> <asp:button
" Server "id=" Sortbycategoryname "
 text=" Sort by Category "/> <asp:button runat=
" Server "id=" Sortbysuppliername "
 text=" Sort by Supplier "/>

Create the Click event handler for all three button. Set the startRowIndex to 0,sortexpression to the appropriate value and rebind the data to 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 ();
}

Now all the work is done! Some of the steps to implement custom paging and sorting are similar to the default paging. Figure 18 shows the last page of data when sorted by category.


Figure 18: Last page of data sorted by category

Note: In the previous example, the sort expression is "suppliername" when sorted by supplier. However, we need to use "CompanyName" when performing custom paging. This is because customizing the paging stored procedure –getproductspagedandsorted– the sort expression to Row_number (), row_number () requires an actual column name, not an alias. So we have to use COMPANYNAME (a column name for the Suppliers table) instead of using SupplierName (the alias in the SELECT statement) as expression.

Summarize

Neither DataList nor Repeater offers built-in sort support, but with a custom interface and a little bit of code, we can do that. When only sorting is implemented (without paging), sort expression can be passed to the ObjectDataSource Select method by DataSourceSelectArguments object. The SortExpression property of the DataSourceSelectArguments object can be assigned in the ObjectDataSource electing event handler.

The easiest way to add sorting to a DataList or repeater that already has a sort function is to add a method to receive the sort expression in BLL. This information can then be passed through the ObjectDataSource selectparameters parameters.

I wish you a happy programming!

Author Introduction

Scott Mitchell, author of this series of tutorials, has six asp/asp. NET book, is the founder of 4GuysFromRolla.com, has been applying Microsoft Web technology since 1998. You can click to see all Tutorials "[translation]scott Mitchell asp.net 2.0 data tutorial," I hope to learn asp.net help.

Related Article

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.