Sorting in DataList
page 1 of 1
Published: 17 Oct 2003
Unedited - Community Contributed
Abstract
We all know that, the DataList web server control does not support in-built sort option. Still we can provide users with the sort option for all columns in a DataList. The logic is very simple. The DataView object has a property called Sort. We are going to make use of this property to sort the rows in a DataList. For our example, we will consider the table, stores from the database (SQL Server 2000), pubs.
by Jesudas Chinnathampi (Das)
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 53822/ 62

Sorting in DataList

Written on: July 22nd, 2002.
Introduction

We all know that, the DataList web server control does not support in-built sort option. Still we can provide users with the sort option for all columns in a DataList. The logic is very simple. The DataView object has a property called Sort. We are going to make use of this property to sort the rows in a DataList. For our example, we will consider the table, stores from the database (SQL Server 2000), pubs.

Aspects that we will be learn in this article.
  1. How to populate a DataList?
  2. How to build the User Interface for Sorting in a DataList?
  3. How to make user of the Sort property of DataView?
  4. An alternate solution for the Sort property!

Populating the DataList

For our example, we will take the table STORES in the PUBS Database. Since stored procedures are very much better than inline query, we use a stored procedure called, sp_stores_sel, which contains a single SQL statement. The SQL statement would be Select * from stores. And finally, we need to bind the DataView to the DataList.

How to build the User Interface for Sorting in a DataList?

We are going to provide hyperlinks for all column headings. So, user can click any column they like. The column clicked will be sorted. As simple as that. When user clicks on any column, we will invoke a Server Side method. The server side method will bind the datalist and will also sort the datalist. We will also provide the user with a DropDownList, in which, they can select the type of sort they need, typically Ascending or Descending. Let us take a look at the Header template to see how we can provide hyperlinks to column headings.

Code for HeaderTemplate of the DataList.
<HeaderTemplate>

    <table width="100%" style="font: 10pt verdana" cellpadding=0 cellspacing=0>
    <tr style="background-color:DFA894">
        <th align=left><a href="datalistsort.aspx#this" onserverclick="SortStoreID" runat="server">Store ID</a></th>
        <th align=left><a href="datalistsort.aspx#this" onserverclick="SortStoreName" runat="server">Store Name</a></th>
        <th align=left><a href="datalistsort.aspx#this" onserverclick="SortStoreAddress" runat="server">Address</a></th>
        <th align=left><a href="datalistsort.aspx#this" onserverclick="SortStoreCity" runat="server">City</a></th>
        <th align=left><a href="datalistsort.aspx#this" onserverclick="SortStoreState" runat="server">State</a></th>
    </tr>

</HeaderTemplate>

How it works?

Important Note: The name of the file that I have used in the href property is "datalistsort.aspx". You should replace this with your aspx filename, unless you keep the filename as myself.

We create a hyperlink for each column names. The target (href) of the hyperlink is set to name of the aspx page itself. Then, we set the property, OnServerClick. For the first column, StoreID, we invoke a server side method called, SortStoreID. This method will populate the DataList and will also sort the rows based on the SortID. The logic is the same for all other columns. Let us take a look at the ServerSide method, SortStoreID.

SortStoreID Method.
Public Sub SortStoreID(ByVal s As Object, ByVal e As EventArgs)
    SortDataList("stor_id " & cboSortType.SelectedItem.Value)
    lblStatus.Text = "Currently Sorted on StoreID: " & cboSortType.SelectedItem.Text
End Sub

Private Sub SortDataList(ByVal strSort As String)
    Dim objConn As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
    Dim objCmd As New SqlDataAdapter("exec sp_stores_sel", objConn)
    Dim objDS As New DataSet()
    Dim objDT As New DataTable()
    Dim objDV As New DataView()

    objCmd.Fill(objDS)
    objDT = objDS.Tables(0)
    objDV = objDS.Tables(0).DefaultView

    objDV.Sort = strSort

    dList.DataSource = objDV
    dList.DataBind()
    objConn.Close()
End Sub

How it works?

Inside the method, SortStoreID we invoke another private method, SortDataList which accepts a string as the argument. This argument is nothing but the sort criteria for the DataList. The method, SortDataList is the heart for the sorting. Apart from using the DataSet, we make use of DataTable and DataView. DataView has a property called, sort. This property takes a sort expression as the input. Sort Expression is made up of the field name and the sort type. Sort type can be either ASC or DESC, which stands for ascending and descending respectively.

An alternate solution for the Sort property!

Instead making use of the SORT property, we can do the sorting in the Stored procedure itself. All we need is to pass necessary arguments to the stored procedure. The necessary argument is nothing but the field name that needs to be sorted and the sort type. So, if you have some time, try this alternative.

Sample output of our scenario

Sorting in DataList - 6614 bytes
Fig: Sorting in DataList.

Test this Script

Download the code

Click here to download the ASPX page
Click here to download the Stored Procedure

Links

Paging in DataList

Conclusion

Thus, we have gone through how to provide sort option for the DataList.

Send your comments to das@aspalliance.com        



User Comments

Title: Sorting not applying   
Name: John
Date: 2012-10-19 7:56:22 AM
Comment:
Respected sir,
I have data in datatable with order asc but not showing in datalist.
Title: sort asp:datalist using jquery   
Name: jitendra
Date: 2011-07-25 3:32:14 AM
Comment:
But , is it possible to sort asp:datalist using jquery?
it's really need.
Title: This Article   
Name: Yash
Date: 2011-05-25 5:58:57 AM
Comment:
Excellent
Title: This Article   
Name: Isaac
Date: 2010-01-19 5:59:51 AM
Comment:
Good
Title: Congrats   
Name: Ronald
Date: 2009-11-05 3:52:48 PM
Comment:
this is really good, thanks for you guidance, keep going.
Title: Very Good   
Name: Sabyasachi
Date: 2009-01-13 2:22:20 AM
Comment:
Its very good example for beginer in .net application.
Title: Very Good   
Name: Özgür
Date: 2008-06-23 4:00:32 PM
Comment:
Really good logic and article. Thanks a lot...
Title: Very good   
Name: Rakesh Jain
Date: 2008-06-10 6:03:16 AM
Comment:
Thanks very much... Can u plz tell me that The Dataview is showing the field in ascending order only.. but i need both ascending and descending order on click event...
Title: Execllient   
Name: Narendra Guatam
Date: 2008-01-01 3:12:44 AM
Comment:
Hai i m Narendra This DataList Paging is Working is Very Fine
Thanks
Title: Good   
Name: Ashraf
Date: 2007-11-24 12:59:53 PM
Comment:
Its a very good article with code, please can you also explain how you have get the connection string values from Web.config file.
Regards
Mirza Ashraf
Title: Thank Yeah!   
Name: sandy
Date: 2007-11-16 9:41:35 AM
Comment:
Perfect, I'll never will thought that it was so easy.

Programmers learn better with an example!!!!!!!
Title: Stay Current   
Name: Fox
Date: 2007-08-01 9:57:51 AM
Comment:
Since stored procedures are very much better than inline query

Wow, statements that are six to seven years, and several versions out of date (for SQL Server anyways) tend to make me doubt your credibility. Please only comment on things you are current on.
Title: good   
Name: sunny
Date: 2007-06-02 1:21:13 AM
Comment:
your code is all code is good sort,paging.
Title: data list control   
Name: uqn
Date: 2007-04-04 8:35:32 AM
Comment:
thanks for the example it was so usefull to me .. realy I made some changes to the code to make it work .. but it was greate specially when I compined it with the code of paging ..I get good results
Title: mr   
Name: daniel leader
Date: 2006-10-12 9:19:35 AM
Comment:
Thanks for the example, very helpful!!
Title: Great Artical   
Name: Ashutosh shukla
Date: 2006-08-21 12:30:09 AM
Comment:
Thats really very very great artical for me.
Thanks a lot
Ashutosh shukla
Software engineer
91-9350133535
Title: why not in c# ?   
Name: taleshi
Date: 2006-08-02 8:49:17 PM
Comment:
hi,
I need all of codes about Database but with c#.
whould you please send me the c# code?
sd_taleshi@yahoo.com
Title: Good work done but ???   
Name: Faheem
Date: 2006-05-20 10:48:48 AM
Comment:
it is good to start sorting in datalist but it does not do sorting on first click in descending and then on second click in ascending.
Title: how can change sort direction ?   
Name: elham
Date: 2006-02-22 2:24:17 AM
Comment:
this sample is very good but how can change sort direction ?
Title: Displaying datagrid in web form   
Name: Friend
Date: 2005-05-05 12:59:05 PM
Comment:
Hi Larbi,
When you bind datagrid and if the datasource is empty then data grid is not displayed.
Thanks
Title: Displaying datagrid in web form   
Name: Larbi
Date: 2004-10-21 6:53:07 PM
Comment:
Hy,
Excuses my bad english. I wish to ask you how to display
datagrid in web form, because, when i run the program, internet explorer display all controls except the datagrid.

Thank you very much.
Larbi

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-05-28 6:20:33 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search