Paging in DataList
page 1 of 1
Published: 17 Oct 2003
Abstract
Today, we will learn how we can add the paging feature for the datalist. It should be noted that, like DataGrid, DataList does not support inbuilt paging mechanism. The essence of this article is how we make use of the object SqlDataAdapter. This object has a method called, Fill which is used to add or refresh the rows in a DataSet. Actually the method Fill is overloaded. We will be mainly concentrating the one which takes four arguments. The four arguments are DataSet, startRecord, maxRecords and the TableName. Second and third arguments are integer. Where as the TableName is the table name. So, if we say objDV.Fill(objDS, 0, 5, "sales"). The dataset will be filled with 5 records and the starting position will be from the first record. First, we are bringing the entire records from the table sales, and then we filter those with the help of startRecord and maxRecords. For our example, we will consider the table, Sales from the pubs database (SQL Server 2000).
by Jesudas Chinnathampi (Das)
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 84162/ 1036

Paging in DataList

Written on: July 23rd, 2002.
Introduction

In my previous article, we discussed how to Sort data in a Datalist. Today, we will learn how we can add the paging feature for the datalist. It should be noted that, like DataGrid, DataList does not support inbuilt paging mechanism. The essence of this article is how we make use of the object SqlDataAdapter. This object has a method called, Fill which is used to add or refresh the rows in a DataSet. Actually the method Fill is overloaded. We will be mainly concentrating the one which takes four arguments. The four arguments are DataSet, startRecord, maxRecords and the TableName. Second and third arguments are integer. Where as the TableName is the table name. So, if we say objDV.Fill(objDS, 0, 5, "sales") The dataset will be filled with 5 records and the starting position will be from the first record. First, we are bringing the entire records from the table sales, and then we filter those with the help of startRecord and maxRecords.

For our example, we will consider the table, Sales from the pubs database (SQL Server 2000).

Things that we will be learning in this article.
  1. How to populate a DataList?
  2. How to build the User Interface for Paging in a DataList?
  3. How to make user of the Fill method of SqlDataAdapter?
  4. An alternate solution for the Fill method!

Populating the DataList

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

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

Apart from the DataList web server control, we will provide user with four hyperlinks for navigation. When the user clicks on any othe four navigation links, we will invoke a server side method which will pulls out the proper records. We will also show the total number of records, total pages and the current page number.

Code for Navigation Links.
<table width=100% align="right">
    <tr>
        <td width=76% align=left>
        <asp:label ID="lblStatus"
        Runat="server"
        Font-Name="verdana"
        Font-Size="10pt" />
        </td>

        <td width=6%>
        <a href="datalistpaging.aspx#this"
        ID="hrefFirst"
        onserverclick="ShowFirst"
        runat="server"><<<</a>
        </td>

        <td width=6%>
        <a href="datalistpaging.aspx#this"
        ID="hrefPrevious"
        onserverclick="ShowPrevious"
        runat="server"><<</a>
        </td>

        <td width=6%>
        <a href="datalistpaging.aspx#this"
        ID="hrefNext"
        onserverclick="ShowNext"
        runat="server">></a>
        </td>

        <td width=6%>
        <a href="datalistpaging.aspx#this"
        ID="hrefLast"
        onserverclick="ShowLast"
        runat="server">>></a>
        </td>
    </tr>
</table>
How it works?

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

We have four hyperlinks for each navigation. On each of these, we have a OnServerClick event which will be fired immediately when the user clicks the link. For example, when the user click the link, <<, which is for the first page, the method ShowFirst will be invoked. All we have to do in the ShowFirst method is to set the current record position to zero. And ofcourse, we have to bind the datalist. Let us take a look at the methods, ShowFirst and DataBind.

ShowFirst and the DataBind method.
Public Sub ShowFirst(ByVal s As Object, ByVal e As EventArgs)
    intCurrIndex.Text = "0"
    DataBind()
End Sub

Private Sub DataBind()
    Dim objConn As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
    Dim objDA As New SqlDataAdapter("exec sp_das_sales_sel", objConn)
    Dim objDS As New DataSet()

    If Not Page.IsPostBack() Then
        objDA.Fill(objDS)
        intRecordCount.Text = CStr(objDS.Tables(0).Rows.Count)
        objDS = Nothing
        objDS = New DataSet()
    End If

    objDA.Fill (objDS, Cint(intCurrIndex.Text), CInt(intPageSize.Text), "Sales")

    dList.DataSource = objDS.Tables(0).DefaultView
    dList.DataBind()
    objConn.Close()
    PrintStatus()
End Sub

How it works?

In the ShowFirst method, we are setting the value of intCurrIndex.Text to be zero. intCurrIndex is a hidden label control which keeps track of the current record number. We also do have two more hidden label controls. They are intPageSize and intRecordCount.

In the method, DataBind you can see that, we get the total record count. We are storing this count in the hidden label web server control called, intRecordCount. Then we use the Fill method to retrieve the current page.

An alternate solution for the Fill method!

The major disadvantage of our logic is that, if we have 1000 records in the table, we are bringing all those to our ASPX pages. All we need is the records for our current page. This can be achived by modifying our stored procedure. We should have an identity column in our table. Then, we should pass the starting position and the number of records to be retrieved to the stored procedure. By this way, we will just be bringing in the needed records, which will decrease the network traffic and throughput time.

Sample output of our scenario

Paging in DataList - 25,698 bytes
Fig: Paging in DataList.

Test this Script

Download the code

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

Links

Sorting in DataList

Conclusion

So, that is it. We have a datalist with paging mechanism. Also read my other article which explains adding the sort feature to a datalist.

Send your comments to das@aspalliance.com


Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 8 and 2 and type the answer here:

User Comments

Title: datalist paging in asp.net   
Name: ramesh
Date: 7/4/2008 9:36:08 AM
Comment:
sir,
iam working with datalist control and
iam getting page numbers in datalist control pls help me
d.ramesh09@gmail.com
Title: Thanks Very Much!   
Name: WFL
Date: 4/8/2008 1:14:20 PM
Comment:
I want to thank you Jesudas, this article was extremely helpful, and saved me a lot of headache and time.
Title: Nice Article   
Name: KarthiKeyan
Date: 3/24/2008 9:07:33 AM
Comment:
Nice article helpful congrats
Title: Nice solution, only one bug is there   
Name: Abhishek Joshi
Date: 3/18/2008 6:30:44 AM
Comment:
It is very nice solution.
The only bug is that when we click on Page-Next link
and if we click on and after coming to last page if we
still click then blank page occurs. In this case the last
page should remain.
Title: Good Article   
Name: KarthiKeyan
Date: 3/18/2008 5:51:30 AM
Comment:
Good Article very Help Ful in my project.
Title: Excellent!   
Name: Rajesh
Date: 3/13/2008 6:24:44 AM
Comment:
it is a very good article. It is a very good technique
Title: Paging in DataList   
Name: Althaf Moideen Konnola
Date: 2/26/2008 11:37:03 PM
Comment:
That's very good code.
This helped me a lot.
Thanks
Title: pagination in datalist   
Name: abhinav
Date: 2/19/2008 12:37:30 AM
Comment:
Can u please help me to change the link btns to numeric.. like 1 2 3 4 5 6 7 ...
Title: paging in datalist   
Name: chirag gohel
Date: 1/31/2008 12:11:20 AM
Comment:
good, i want insrt image in datalist, how i can do it?
Title: hello   
Name: pradeep
Date: 1/18/2008 5:21:55 AM
Comment:
Very much Usefull..
Title: paging   
Name: Rupa
Date: 12/18/2007 8:03:19 AM
Comment:
i have a question for u, i did paging in my application succesfully but i have a problem

i have 200 records, per page i kept 5 records, so i am getting 40 page numbers i.e(1 2 3 4 5 6 7 .....40), i want to restrict the page numbers to (1 2 3 4 5) then Clicking on the next i want to get (6 7 8 9 10) like that

please can u help me
Title: great   
Name: snehasis mishra
Date: 12/5/2007 2:11:26 AM
Comment:
Ya,ofcourse it's a brilliant work but if they have done in c# and also the paging has done like 1,2,3.Then it might be much better.Still it's a marvolous work.
Title: Very Good   
Name: Ravindra Thakur
Date: 9/12/2007 3:13:01 AM
Comment:
this is very good and working fine but rename the databind method with databind1 or another name and replace the name with use the databind method with new name. But leave dList.DataBind() as same.
Title: Datalist paging   
Name: Anderson Theobaldo
Date: 8/1/2007 3:22:27 PM
Comment:
That's very good code.
This helped me a lot.
Title: Nice code   
Name: Helder Lima
Date: 7/16/2007 10:57:26 AM
Comment:
Very good,

This code helped me a lot!

Thanks in advance,
Helder Lima
São Paulo - Brazil
Title: datalist Paging   
Name: Vivekk umar
Date: 7/11/2007 1:43:57 AM
Comment:
it was really helpful...
i hav understood the concepts
thnx
Title: Datalist Paging   
Name: Sanjay Kadam
Date: 5/17/2007 5:51:28 AM
Comment:
That's very good code.
This helped me a lot.
Title: Datalist paging   
Name: sarayu
Date: 5/15/2007 2:28:05 AM
Comment:
how to do paging in datalist using page numbers like (1,2,3,4) in asp.net with vb.net code
Title: had to post 2   
Name: some one
Date: 3/15/2007 12:44:27 PM
Comment:
ooh ... and he only published this in 2003, so I'm sure 4 years later he still checks it just so he can help 5% of the morons that can't get it to work.

lol ...
Title: had to post   
Name: some one
Date: 3/15/2007 12:42:17 PM
Comment:
Why do people post their problems in User Comments?

lol it's so funny.

When 95% of the people can get it to work ... umm .. the problem is most likely you not the author's code.

He took the time to provide a working useful example, why don't you take the time to debug your errors and fix them yourself.

Get a clue.
Title: erro   
Name: sc
Date: 2/25/2007 6:23:38 PM
Comment:
I keep getting this error:
Input string was not in a correct format.

on this row:
If CInt(intCurrIndex.Text) + 1 < CInt(intRecordCount.Text) Then
Title: Very nice   
Name: Vinod Kushwaha
Date: 2/20/2007 1:48:04 AM
Comment:
This is very nice.
Title: nice   
Name: L RamaKrishna Yepuri
Date: 2/19/2007 5:08:18 AM
Comment:
God to see it
Title: Error   
Name: Ramesh
Date: 2/6/2007 5:08:34 AM
Comment:
\
\
\
\
\
\
\
\
\
\
Title: datalist   
Name: monawwar hussain
Date: 2/2/2007 11:50:29 PM
Comment:
thank for this example really it solve my problem in just few minute.
Title: excellent   
Name: vineeta
Date: 12/29/2006 12:46:29 AM
Comment:
this article is excellent.whole article is very clear and userfriendly
Title: Use ViewState instead of invisible controls   
Name: Dave Knipper
Date: 12/28/2006 3:58:29 PM
Comment:
I posted a longer comment but I received an error, so here's a shorter one.

Replace invisible controls such as (Label)intCurrIndex with ViewState["intCurrIndex"] to remove overhead and clean up the code.

If people want my c# version using viewstate let me know and I'll post somewhere.
Title: Paging in DataList   
Name: Mr. Manoranjan Singh
Date: 11/27/2006 7:24:15 AM
Comment:
This code for us very very useful.
Thanx a lot.
Title: Not getting result   
Name: Mohd. Faiz
Date: 11/22/2006 6:34:37 AM
Comment:
I have used as per your refereced but i am not getting result. you can see at www.thawrani.com.

after that i have used two link buttons but now same so please provide right guidance to me.
Title: THanks alot   
Name: Myo Aung
Date: 11/8/2006 6:30:26 AM
Comment:
Very useful
Title: Paging In Datalist   
Name: Gopakumar
Date: 10/9/2006 6:39:48 AM
Comment:
Very Good article, thanks a lot for this asp.net coding...

gopintl@rediffmail.com
Title: very neice   
Name: Ghanshyam
Date: 9/21/2006 2:57:22 AM
Comment:
this is very helpful for ASP.NET developer
Title: Thank You   
Name: David Mercer
Date: 9/13/2006 5:45:17 PM
Comment:
I have been searching for this for quite some time. I found a lot of explanations but none worked. Yours was easy to understand and worked great. Thanks...
Title: fabulas   
Name: niranjan
Date: 8/25/2006 6:10:57 AM
Comment:
thank u , that helped me alot
Title: thanks   
Name: Harish.G.M
Date: 8/9/2006 7:29:37 AM
Comment:
thanks man this helped me a lot
Title: Thank you very much   
Name: ctavan_it@...
Date: 8/7/2006 6:29:09 AM
Comment:
The article is very useful for me !!
Thank your ...
Title: Ur article is very nice   
Name: Nagendra
Date: 8/2/2006 11:06:10 AM
Comment:
Your artice help me a lot and thanks to u and ur article.
Title: Nice   
Name: Kavitha
Date: 7/27/2006 6:17:48 AM
Comment:
Thanks for the Simple Code
Title: Datalist pagin   
Name: Kaushal
Date: 7/22/2006 3:00:26 AM
Comment:
its gud .
but could u plz post full procedure code.
Title: Nice   
Name: kataria Bipin
Date: 7/17/2006 1:25:51 AM
Comment:
than's for making this type of good code.
Title: Good article   
Name: Bob Smith
Date: 7/10/2006 5:04:15 PM
Comment:
Good article, thanks.
Title: Passing Parameters into procedure   
Name: gym
Date: 7/3/2006 3:56:44 PM
Comment:
Excellent article-
I am trying to pass a value into my stored procedure. The first time that the page loads I can see the first set of records, but when I press the next link, i see the error that my stored procedure is expecting the parameter. It looks like it is not storing the parameter that was passed in in the dataset. Please help. How do I do this?
Title: Really..........   
Name: Anubhav Singh
Date: 6/24/2006 9:27:37 AM
Comment:
Its so helpfull for paging in datalist.
Title: very helpful   
Name: Ranjeet
Date: 6/7/2006 3:43:55 AM
Comment:
This is very helpful for me. this absolutely nice
Title: Very good   
Name: Vijay Kumar Bandari
Date: 5/30/2006 1:47:34 PM
Comment:
Hi Das,

This article helped me alot and i learned a new feature..
thanks alot
Title: Thanks   
Name: Rick
Date: 5/9/2006 11:08:41 AM
Comment:
Great article, very simple compared to some other methods and more efective.
Title: gr8 job!!   
Name: Rajesh
Date: 5/5/2006 5:43:23 AM
Comment:
This article is very helpful, again thanks alot!!
Title: thanx bro   
Name: Alex
Date: 4/19/2006 4:43:31 AM
Comment:
Thank you, this really helped me.
Title: Sometimes an Extra Page   
Name: Kevin M
Date: 4/2/2006 9:46:23 AM
Comment:
I have pagesize set to 20 yet on some records I get an extra page (blank) even though the 'Total Records' is always correct.
I don't understand why it does it some times.
On one page which has 6 recors then '1 of 1' is shown correctly BUT on another page that has 11 records then it shows '1 of 2' when there is clearly only one page.

Amy ideas?

Also, how do you make the 'first' amd 'last' navigation links invisible/disabled when you are on the first or last page.

Thanks
Title: C# Version - Again   
Name: Darryl Wright
Date: 3/29/2006 3:31:01 AM
Comment:
I see there are numerous people who are looking for a C# version of this amazing paging mechanism for the Datalist Web Control. I see too that someone
Name: Mark
Date: 1/12/2005 6:14:41 PM
Comment:I converted to C# cant post? must not allow HTML etc

Does anyone know where this C# code can be found?
Title: numeric like 1 2 3 4 5   
Name: Kanchi
Date: 3/6/2006 10:37:02 PM
Comment:
Can u please help me to change the link btns to numeric.. like 1 2 3 4

thanks
Title: version in c#   
Name: christian
Date: 3/2/2006 3:28:59 AM
Comment:
Good day....
its very helpful to me...but i just want to know if there is a version for c#..thanks
Title: Have method Problem   
Name: Dave
Date: 2/13/2006 12:49:37 AM
Comment:
I have not been able to get the code running in ASP.Net 2.0 The error is "DataBind() shadows an overrideable method in the class control.." The page loads but the data is not presented. Any help would be appreciated.
Title: I have a question please   
Name: Elnahrawi
Date: 2/4/2006 9:36:56 AM
Comment:
Thank you

I have a question about this article

How can i use this way to paging the datalist but with using a "HyperLink" controls instead of "ButtonLinks" cuz the search engains can't read the java links and it dosent access the other pages

if you need a live example you can visit my wibsite http://www.books-download.com/
and see what i mean

Thanks in advance
Title: Great   
Name: John S.
Date: 1/16/2006 11:01:52 AM
Comment:
Thanks for this! Simple but effective.
Title: Mr   
Name: Welker
Date: 1/5/2006 5:06:06 AM
Comment:
Thanks !
Title: about datagrid pagging   
Name: Rakesh Lal Dewangan
Date: 1/5/2006 2:10:27 AM
Comment:
Thanks
a lot ,
I have used this code ,my page is properly working .

Regards
Rakesh Lal Dewangan
Title: Helpful   
Name: Nirmal
Date: 12/6/2005 5:36:25 AM
Comment:
Thanks, That helped a lot.
Title: Excellent   
Name: Sajjad
Date: 11/20/2005 3:13:54 AM
Comment:
This is Excellent,solvedmy problem
Title: Thanx   
Name: Jikku
Date: 9/26/2005 2:02:14 AM
Comment:
Hi,

It helped me lot!!!

Bye
Jikku
Title: paging with datalist   
Name: abhishek sachan
Date: 8/10/2005 4:30:12 AM
Comment:
good logic,help me alot
thank you
Title: short and sweet   
Name: kavita singh
Date: 7/20/2005 12:10:32 AM
Comment:
thanks a lot i found this code at the right time when i needed it.
Title: Fine Article   
Name: P.Suresh
Date: 7/4/2005 1:30:53 AM
Comment:
It is is very nice.Even if we pass the starting position and the number of records to be retrieved to the stored procedure , how we retrieve those records through query.Plz send the stored procedure.
I have a small requirement.
I want the data in the table like this
-----------------------------------
store Id :7877
Order No :ord1111
qty :344
------------------------------------
store Id :45545
Order No :oed333
qty :555
------------------------------------
with paging. Is datalist solves my problem.if you know how plz mail me at PSureshMsc@yahoo.co.in.
Title: good boy   
Name: kamran
Date: 6/23/2005 10:47:34 AM
Comment:
it's a kind of nice code, thanks alot
Title: It realy nice   
Name: Vineet Kaushik
Date: 6/16/2005 9:08:59 AM
Comment:
Hay,
It's realy help me.
Thanks thanks alot.
Title: need clarification   
Name: nag
Date: 6/7/2005 2:40:22 AM
Comment:
can we have numeric paging like 1 2 3 4 5...
if yes,do u have the code for that....
Title: Brilliant Work   
Name: Aman
Date: 5/14/2005 5:02:19 AM
Comment:
Its marvellous.We r highly thankful to u.
Title: postback   
Name: freggel
Date: 4/21/2005 8:47:59 AM
Comment:
my intCurrentIndex is always reset to zero with a postback and because of this the previous and next (buttons) doesnt work, does anyone know a good solution for this issue.

Thanks
Title: Paging   
Name: bikash
Date: 4/15/2005 10:44:06 PM
Comment:
Good one.More helpfull
Title: Yikes...   
Name: Dot net
Date: 4/13/2005 11:11:10 AM
Comment:
This is a very slow method of doing it... if you have 1,000,000 records it will query all 1,000,000 records each time you switch pages... May i recommend using a paged stored procedure... it will be 100x faster.
Title: Thanx   
Name: Swarup Kumar
Date: 4/9/2005 7:48:01 PM
Comment:
Sir,
Thank you so much for helping the developer community by sharing the knowledge. I have implemented paging in datalist successfully by getting the help from your article.

Swarup Kumar
INDIA
Title: hsms   
Name: hsms
Date: 4/8/2005 9:27:59 AM
Comment:
We implemented the script provided by you on one of our project but we are facing a problem and need your help for the same, the prolem is as follows:


The next button doesnt seem to work properly say we have 7 records and we set paging to 5, it shows Showing 1 of 2 which is correct but again when we click the next button it displays showing 2 of 2 but if now we again click the next button it says showing 3 of 2 which isnt correct coz there arent any more records and as such a blank page is displayed.


we are sending you the code we are using
Title: good code   
Name: rupesh
Date: 4/7/2005 12:41:38 AM
Comment:
good code working properly but how will i be able to do paging in numeric mode like 12345 next 5 etc
Title: tq   
Name: sista
Date: 3/25/2005 10:43:21 AM
Comment:
works fine, thanks :)
Title: Excellent   
Name: Ravi
Date: 3/16/2005 11:09:50 AM
Comment:
This is one of the methods to cheat Microsoft!!.. Excellent work.. U got good logic
Title: developer   
Name: mohamed ali
Date: 3/5/2005 4:26:32 PM
Comment:
it is really helpful
Title: feedback   
Name: hazri
Date: 2/5/2005 3:24:20 AM
Comment:
Wow its nice... but how to make button navigation like yahoo search result. I mean in that button use number, not arrow. If have 5 page show 1 2 3 4 5
Title: Using footer   
Name: xxx
Date: 1/28/2005 10:19:06 AM
Comment:
Is it possible to place the navigation and Total Records in the footer of the datalist?
Title: thanks   
Name: apple
Date: 1/19/2005 9:55:04 AM
Comment:
123
Title: I coverted to C# Version   
Name: Mark
Date: 1/12/2005 6:14:41 PM
Comment:
I converted to C#, cant post? must not allow HTML etc

sent to das maybe he can
Title: C# Version   
Name: Mark
Date: 1/12/2005 6:03:57 PM
Comment:
test
Title: just cool   
Name: Vidyadhar
Date: 12/24/2004 2:13:07 AM
Comment:
its very beautifully.
Title: Good   
Name: Seyed Amin Pouriyeh
Date: 12/15/2004 12:53:20 AM
Comment:
Thanks Dear,
Title: Suggestion   
Name: Sravya
Date: 11/27/2004 12:40:00 AM
Comment:
Sir,

It is ok, but if you give full code it will helpful more to users. i mean giving code for each and evey button clearly, explaining the purpose of each code. where you use that hiddden buttons, how to display total record numbers etc.,

Thanking you
Title: Nice but what about Sorting And Paging Simeltanously !!!!!!!!   
Name: Mahsa
Date: 11/2/2004 2:38:50 PM
Comment:
we can not reserve our sort in this way during the postback
of pages,what is the solution then?

Again Nice Work
Title: very useful   
Name: me
Date: 8/31/2004 4:06:12 AM
Comment:
this article is very useful.
Title: not work if put before datalist   
Name: Jammy
Date: 8/25/2004 11:42:14 AM
Comment:
Look this code is fine but it not work if u put the
<< < > >> before the datalist .
Title: Thanks a bunch!   
Name: Nicholas Ho
Date: 8/22/2004 4:36:37 AM
Comment:
Just want to say thanks for writing this extremely helpful article. I have been searching everywhere for weeks and your article has got be the best.

Love your work,

Nicholas
Title: Excellent   
Name: Davut Engin
Date: 8/21/2004 12:14:25 PM
Comment:
Thank you for your clever help to DataList users :)
Title: This is not woking out   
Name: Manish
Date: 8/5/2004 4:55:25 PM
Comment:
The code in this article is not working out. Plz check it out again.
Title: good   
Name: rajarajan
Date: 8/5/2004 7:18:25 AM
Comment:
its really helped me
Title: Very Nice   
Name: Jerome Lavoie
Date: 8/3/2004 11:00:43 AM
Comment:
Thanks, that helped a lot






Ads Powered by Lake Quincy Media
Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2008 ASPAlliance.com  |  Page Processed at 7/5/2008 12:53:19 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search