Introduction
This article describes DataList Paging with Next, Previous, First and Last page access functionality. A numbers list is also displayed for accessing pages faster.
Background
A few days ago when I was required to implement custom paging in a DataList (like Google paging) I searched the Internet but did not find any perfect code. Then after some effort I wrote my own code. I decided to share it with other users. Surely this will help other developers.
Using the code
Here we will explain the complete example code.
HTML in Default.aspx contains the following two DataLists:
-
dListItems (Main DataList showing actual contents to implement paging)
-
dlPaging (used for displaying page numbers as navigation links)
Four LinkButtons to move to the next, previous, first and last page.
A Label to display the current page number out of a total number of pages.
Now we come to the C# code. There are the following three properties:
1. CurrentPage (to maintain the current page index)
2. fistIndex and 3-lastIndex (These two properties are used for paging the DataList)
private int CurrentPage
{
get
{
object objPage = ViewState["_CurrentPage"];
int _CurrentPage = 0;
if (objPage == null)
{
_CurrentPage = 0;
}
else
{
_CurrentPage = (int)objPage;
}
return _CurrentPage;
}
set { ViewState["_CurrentPage"] = value; }
}
private int fistIndex
{
get
{
int _FirstIndex = 0;
if (ViewState["_FirstIndex"] == null)
{
_FirstIndex = 0;
}
else
{
_FirstIndex = Convert.ToInt32(ViewState["_FirstIndex"]);
}
return _FirstIndex;
}
set { ViewState["_FirstIndex"] = value; }
}
private int lastIndex
{
get
{
int _LastIndex = 0;
if (ViewState["_LastIndex"] == null)
{
_LastIndex = 0;
}
else
{
_LastIndex = Convert.ToInt32(ViewState["_LastIndex"]);
}
return _LastIndex;
}
set { ViewState["_LastIndex"] = value; }
}
We need a PagedDataSource object to get and set various properties for custom paging.
PagedDataSource _PageDataSource = new PagedDataSource();
The GetDataTable() method simply returns a DataTable. In the example code I am not pulling data from the database. I have created a custom DataTable and added 100 rows to the DataTable using a for loop.
private DataTable GetDataTable()
{
DataTable dtItems = new DataTable();
DataColumn dcName = new DataColumn();
dcName.ColumnName = "title";
dcName.DataType = System.Type.GetType("System.String");
dtItems.Columns.Add(dcName);
DataRow row;
for (int i = 1; i <= 100; i++)
{
row = dtItems.NewRow();
row["title"] = "Sample Row: I am putting here sample text for row " + i;
dtItems.Rows.Add(row);
}
return dtItems;
}

Prasunjeet SoniPosted Jul 9, 2017, 6:50 AM
Very good logic and it resolve my issue .. thanks a lot Rizwan
Osca TanPosted Nov 18, 2016, 4:09 AM
I did as your instructions, but I can't see anything, except four linkbuttons. I replace datalist to gridview, I can see data. But the line "1 2 .... 99 100" I don't see it. How can I solve it? Please help me. I need your opinions.
Sameh SaeedPosted Jan 14, 2016, 1:47 AM
Many thanks it helps my too much :)
Upendra Pratap ShahiPosted Jun 29, 2015, 5:06 AM
good one
anwar hossainPosted Jun 5, 2015, 8:54 AM
awesome paging example best ..
anwar hossainPosted Jun 5, 2015, 8:06 AM
Thanks for article
Anupam SinghPosted Jul 19, 2014, 12:49 AM
Nice Example. it helps me.. thanx for sharing
sa mePosted Jul 8, 2014, 10:26 AM
very nice thank you sir
pankaj rojalaPosted Jun 17, 2014, 1:44 PM
thank you sir ...(y)
Melike MelekPosted Apr 7, 2014, 5:46 AM
Thank you very very much, I searched such article for 4 days , now I found , it works :)
AleshaPosted Dec 4, 2013, 9:20 AM
Thanks a million.The above article was really very helpful in understanding custom paging within Datalist
behnam pasbanPosted Dec 29, 2012, 8:34 AM
Thanks.:)
behnam pasbanPosted Dec 29, 2012, 8:34 AM
Thanks.:)
sunil patelPosted Dec 15, 2012, 1:00 AM
Thanks..............
Duy KienPosted Nov 8, 2012, 5:44 AM
thank you!
Sumit Singh PanwarPosted Nov 2, 2012, 3:33 AM
Thanks for this article.. i am new in asp.net and i also want to implement the similar kind of paging but i want to show only page no. from 1 to 4 out of 10 pages at a time and when we press next or previous page no will increase or decrease accordingly thanks in advance....
alireza KERAMATIPosted Oct 7, 2012, 6:07 AM
Thanks for this article .
Ahmad HussainPosted Sep 21, 2012, 6:33 PM
excellent work thaks for sharing
rohit jainPosted Aug 22, 2012, 3:38 PM
thanks a lot,....thank u sir its really best post on paging in datalist
Guest UserPosted Aug 21, 2012, 6:18 AM
thanks good one
Raman ChoudharyPosted Aug 15, 2012, 2:18 AM
thank u very much sir...
arun kothiwalPosted Aug 7, 2012, 1:20 AM
i have problem with no. of Page i have 20 pages and it shows like 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 i want to show it like Below… 1 2 3 4 5..20 how can i please help Thanks in advance
Jitender KumareditedPosted Jul 22, 2012, 1:06 PMEdited Jul 22, 2012, 1:07 PM
Great..... This is best post that i ever read about datalist paging.
mas liPosted Jun 30, 2012, 5:49 AM
Great,Thanks alot
ASPNET CODERPosted Jun 21, 2012, 2:57 AM
Thanks ......... :)
ali ashooriPosted Jun 1, 2012, 7:50 AM
Thanks with advance... You just get me ride of a hell with your sample! :)
Kushal TambatPosted May 24, 2012, 8:20 AM
Thanks, it is very useful.
piyush SinghPosted Mar 28, 2012, 8:02 AM
Thank you....it works fine, just of little bit modification...... appreciable work mate's.....
database databasePosted Jan 15, 2012, 4:30 PM
Thank you so much for this is ... it saved me today .. thumbs up (Y)
KevinPosted Jan 2, 2012, 3:51 AM
Hi, Very new to asp and c#. Can you give an example of the code behind and what it replaces in the current example. Many thanks
huuPosted Dec 27, 2011, 11:19 PM
i have multy more 1000 recode? how paging?
karthikPosted Nov 20, 2011, 10:37 AM
I have small qustion, I am creating datalist control through programatically, (it will create more than 5+ datalist) How can we achive the Paging for this each datalist. Please help me.
Sachin GPosted Jul 1, 2011, 4:26 AM
Dear SIr, Thank You For Providing This code, i was searching this type code from last somany days. Once again Thank you, Very much
jarnail singhPosted Jun 2, 2011, 6:23 AM
Thanks sir .......... Very good example
suhail pawanePosted Apr 29, 2011, 1:01 AM
I have implemented the code in my project but datalist i navigating correctly when i click on last,pre,next,first link button. but when i click on the numbers the datalist disappers which means it not navigating And can u please let me know the logic for the constant number (5) u specified in the program Please do reply its urgent
mahipal yadav rewariPosted Mar 15, 2011, 12:13 AM
hi , Rizwan Javed sir your code real perfect . very very thanks of help
Mahmoud AlgoulPosted Feb 1, 2011, 8:19 AM
can any one please tell me how to make it work with Database please.
sanjay guptaPosted Jan 29, 2011, 5:14 AM
Hats off to u.
wachidaPosted Dec 14, 2010, 10:36 PM
You are smart.
shashi bhushan singhPosted Nov 11, 2010, 8:05 AM
Well done sir thanks
nguyen van ThangPosted Nov 2, 2010, 11:32 AM
Thank you very much. Great code :) . I will use in project of me
Ram KumarPosted Sep 16, 2010, 5:53 AM
i am search this type of code of my website. this code is very helpful
ram kumarPosted Jul 26, 2010, 8:07 AM
great i enjoyed it. thanks
shahid abbassPosted Jul 22, 2010, 5:14 AM
I m desktop developer new to web beast. Your code helped me and took me out of a tight spot. Thanks,
HariseditedPosted Jul 2, 2010, 9:14 AMEdited Jul 2, 2010, 9:17 AM
But, i have one issue. i have drop down in my data list, i select some value in first page and i click next button and once again when i came back to previous selected value from drop down is not there, entair Data-list is binding once again. can u help me, advance thanks...
darwin cajamarcaPosted May 31, 2010, 4:10 PM
MUCHAS GRACIAS POR EL CODIGO, LO ESTUBE BUSCANDO POR ALGUN TIEMPO, GRACIAS NUEVAMENTE!!!...
priya saxenaPosted Apr 23, 2010, 3:00 AM
thks for the article ...is this code applicable for pagination in asp.net web services
gayathri RajagopalPosted Apr 20, 2010, 9:12 PM
Hi this is Gayathri. I was looking for the same code. I am using ASP.net with c# in my project. I have a Table Class instead of DataTable. So can i use this same method for my project. If yes what should i specify instead of the code below in BindItemList(): "_pageDataSource.DataSource=dataTable.DefaultView" Can You please help?
amol patilPosted Mar 14, 2010, 6:19 AM
Hey Thank u very much.... Really this code helps me lot...... Good Bless u...........
foramPosted Mar 12, 2010, 12:40 AM
Thanks Man.... It helps me alot....
Iman AlkafouryPosted Nov 24, 2009, 6:54 AM
I used your code , thanks a lot.
asd asdPosted Nov 24, 2009, 1:02 AM
Thanks maan.. it really made my life easy
cihangir surPosted Oct 9, 2009, 9:23 AM
Hi my names cihangir.Iam from Turkey. this is beautiful project. tahnks.
priyankaPosted Sep 22, 2009, 8:06 AM
thanx so much for this code..
summaiya shaikhPosted Jun 24, 2009, 8:40 AM
thank you very much for this article
hhillPosted Jun 15, 2009, 5:54 AM
Hi all, can anyone plz help me how can i page a repeater that is bound to a sql datasource?? any help will be appreciated..:)
troy MeditedPosted Jun 8, 2009, 1:44 AMEdited Jun 8, 2009, 1:52 AM
Hi , I have worked your sample code its not working , instead of gettable function i have used database , first it shows the 10 records then when i click the next navigation link it has not shown the remaining records.. I have converted your code to vb.net if you want the code let me know...?
pramod garudkarPosted May 4, 2009, 6:27 AM
its greate help thank your
joohithaPosted Mar 6, 2009, 11:54 PM
Thank u very much. U know iam serching for the same code for so many days. It's Wroking fine.