Manipulating data in asp.net 2.0 42: DataList and Repeater data Sorting (i) _ self-study process

Source: Internet
Author: User
Tags eval sorted by name visual studio

Introduction

DataList and Repeater data paging we learned how to add paging functionality to the DataList. We created a method named Getproductsaspageddatasource in the Productsbll class that returns a PagedDataSource object. When bound to DataList or Repeater, they will display only the data for the requested page. This technique is similar to Gridview,detailsview,formview's built-in paging functionality.

In addition to pagination, the GridView provides built-in sorting capabilities, and neither DataList nor repeater. However, the sorting function can be achieved by a little bit of code. In this chapter we will learn how to implement sorting in DataList and Repeater, and we will also create a DataList or repeater that supports paging and sorting at the same time.

Review the sort

As we see in pagination and sorted report data, the GridView provides sort support. The field for each GridView can have an associated sortexpression that indicates the field on which the data is sorted. When the AllowSorting property of the GridView is set to True, the header of each field in the GridView containing the SortExpression property is represented as a LinkButton. When you click a header, the page postback, and the data is sorted according to the sortexpression of the Point field. In addition, the SortDirection property indicates whether the data is ascending or descending.

When the GridView is bound to a data source control, it passes SortExpression and sortdirection to the data source control. The data source control gets the data and sorts according to SortExpression and SortDirection. The data is then returned to the GridView.

To implement this function in DataList or repeater, we need to:

Create a Sort interface
Make a note of the sorted fields and orientations (ascending or descending)
Instruct ObjectDataSource to sort by specific fields

We will deal with the above three steps in the third and four steps. Then we'll see how to get DataList or repeater to support these two features (paging and sorting) at the same time.

Step two: Show products in repeater

Before you implement sorting, you first create a repeater that lists all of the product. Open the Sorting.aspx page in the Pagingsortingdatalistrepeater folder. Add a repeater and set the ID to sortableproducts. Create a ObjectDataSource named Productsdatasource from the smart tag. Configure it with the GetProducts () method of the Productsbll class. In the INSERT, UPDATE, delete tab drop-down list, select (None).


Figure 1: Creating ObjectDataSource


Figure 2: Select (None) in the Drop-down list for UPDATE, INSERT, DELETE tab

After binding to a data source, Visual Studio does not automatically create ItemTemplate for repeater, which is not the same as DataList. And since there is no "Edit Templates" option in the smart tag of the Repeater control like DataList, we need to add the declaration code directly. We use the same ItemTemplate as the previous chapter, which displays product name, supplier, category.

Now your repeater and ObjectDataSource declarations should look similar to the following:

<asp:repeater id= "sortableproducts" datasourceid= "Productsdatasource" "enableviewstate="

 False "runat=" Server ">

 <ItemTemplate>

   
 

Figure 3 is how you view the page now.


Figure 3: Display Name of product, Supplier, Category

Step three: Instruct ObjectDataSource to sort the data

In order for the data to be sorted in the repeater, we need to sort the data sorted expression tell ObjectDataSource. Before ObjectDataSource gets the data, the first thing that fires is the selecting event, which gives us an opportunity to specify the sort expression. Selecting event handler has a ObjectDataSourceSelectingEventArgs type parameter that has a name of arguments Properties of the DataSourceSelectArguments type ... The DataSourceSelectArguments class is designed to pass data-related requests from the data consumer to the data source control, which has a SortExpression property.

Create a selecting event handler and pass the sorted information to ObjectDataSource from the ASP.net page with the following code:

protected void productsdatasource_selecting

 (object sender, ObjectDataSourceSelectingEventArgs e)

{

 E. Arguments.sortexpression = SortExpression;

}

SortExpression needs to be assigned the name of the sorted field (for example, "ProductName"). It does not have a sort direction-related attribute, so if you want to sort in descending order, append "DESC" to the SortExpression value (such as "ProductName DESC").

Go ahead and try hard coding. Assign SortExpression to a different value and browse the page. As shown in Figure 4, when using "ProductName DESC" as SortExpression, product is sorted in reverse alphabetical order according to name.


Figure 4:product Sort alphabetically by name

Step Fourth: Create a sort interface and write down sort Expression and Direction

The sort support that opens the GridView converts the header text of each sortable field to a LinkButton, which is sorted when clicked. This sort of ordering is reasonable for the GridView because its data is displayed neatly in the form of columns. For DataList and Repeater, a different sort interface is required. A common list of data (relative to the data grid) is the sort interface that uses a Drop-down list that provides a sort field. We will complete the interface in this chapter.

Add a DropDownList above the Sortableproducts repeater and set the ID to SortBy. In the Properties window, click the Items property to open the ListItem Collection Editor. Add ListItems to let the data be sorted according to ProductName, CategoryName, suppliername fields. Add ListItem at the same time so that product is sorted according to the reverse name order.

The ListItem Text property can be set to any value (such as "name"), but value must be set to the name of the data field (such as "ProductName"). Add the string "DESC" to the name of the data field to allow the result to be sorted in descending order, such as "ProductName DESC".


Figure 5: Adding ListItem for each sortable field

Finally, add a button to the right of the DropDownList. Set the ID to Refreshrepeater,text set to "Refresh".

Once this is done, the DropDownList and button declaration syntax should look similar to the following:

<asp:dropdownlist id= "SortBy" runat= "Server" >

 <asp:listitem value= "ProductName" >name</asp: listitem>

 <asp:listitem value= "ProductName DESC" >name (Reverse order)

  </asp:ListItem>

 <asp:listitem value= "CategoryName" >Category</asp:ListItem>

 <asp:listitem value= "SupplierName" >Supplier</asp:ListItem>

</asp:DropDownList>

<asp:button runat= "Server" id= " Refreshrepeater "text=" Refresh/>

complete DropDownList, we need to update ObjectDataSource selecting event handler, To use the value of the selected SortBy ListItem as the sort expression instead of the previous hard code.


protected void productsdatasource_selecting

 (object sender, ObjectDataSourceSelectingEventArgs e)

{

 //Have The ObjectDataSource sort the results by the selected

 //Sort expression

 e.arguments.sortexpress ion = Sortby.selectedvalue;

}

Now the first time you browse the page, because the default SortBy ListItem value is ProductName, the product is sorted according to the ProductName field. See Figure 6. Select a different item – such as "Category" – and then click Refresh to Postback, and the data will be reordered according to Category name, as shown in Figure 7.


Figure 6: The first products are sorted by Name


Figure 7: Now the products are sorted according to Category

  Note: The refresh button will reorder the data because the view state of repeater is disabled, so repeater is rebind to the data source each time postback. If you open repeater view state, changing the Drop-down list will have no effect on the sort. To fix this problem, you can create an event handler for the Refresh button's Click event to rebind repeater to the data source (invoke the DataBind () method of the repeater).

Make note of sort Expression and Direction (sort expression and sort direction)

If a page containing sortable DataList or repeater may have other and sort-independent postback occurrences, then we need to write down the sort expression and direction during the postback process. For example, we modify the repeater of this chapter to include a Delete button for each product. When the user points Delete button, we execute some code to delete the selected product and then bind the data to repeater. If the sorted information is not saved during the postback process, the displayed data reverts to the original sort state.

In this chapter, DropDownList implicitly saves the sort expression and direction in its view state. If we use a different sort interface –linkbutton provide different sorting options – we need to make a note of the sorted information during the postback process. This can be done by remembering the parameters of the sort in Page view state, or in the QueryString, or by some other status-preserving mechanism.

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.