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;
}The method BindItemsList() gets the DataTable by calling the GetDataTable() method.
Create a DataSource for _PageDataSource, set the PageDataSource properties and at the end bind PageDataSource to dListItems.
private void BindItemsList()
{
DataTable dataTable = this.GetDataTable();
_PageDataSource.DataSource = dataTable.DefaultView;
_PageDataSource.AllowPaging = true;
_PageDataSource.PageSize = 10;
_PageDataSource.CurrentPageIndex = CurrentPage;
ViewState["TotalPages"] = _PageDataSource.PageCount;
this.lblPageInfo.Text = "Page " + (CurrentPage + 1) + " of " + _PageDataSource.PageCount;
this.lbtnPrevious.Enabled = !_PageDataSource.IsFirstPage;
this.lbtnNext.Enabled = !_PageDataSource.IsLastPage;
this.lbtnFirst.Enabled = !_PageDataSource.IsFirstPage;
this.lbtnLast.Enabled = !_PageDataSource.IsLastPage;
this.dListItems.DataSource = _PageDataSource;
this.dListItems.DataBind();
this.doPaging();
}BindItemsList() calls the doPaging() method that actually binds the paging DataList as in the following:
private void doPaging()
{
DataTable dt = new DataTable();
dt.Columns.Add("PageIndex");
dt.Columns.Add("PageText");
fistIndex = CurrentPage - 5;
if (CurrentPage > 5)
{
lastIndex = CurrentPage + 5;
}
else
{
lastIndex = 10;
}
if (lastIndex > Convert.ToInt32(ViewState["TotalPages"]))
{
lastIndex = Convert.ToInt32(ViewState["TotalPages"]);
fistIndex = lastIndex - 10;
}
if (fistIndex < 0)
{
fistIndex = 0;
}
for (int i = fistIndex; i < lastIndex; i++)
{
DataRow dr = dt.NewRow();
dr[0] = i;
dr[1] = i + 1;
dt.Rows.Add(dr);
}
this.dlPaging.DataSource = dt;
this.dlPaging.DataBind();
}protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindItemsList();
}
}dlPaging_ItemCommand sets the CurrentPage Property and again makes a call to BindItemsList() as in the following:
protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName.Equals("Paging"))
{
CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
this.BindItemsList();
}
}dlPaging_ItemDataBound will set Enabled equals to false when binding the LinkButton that is currently clicked from the paging list as in the following:
protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
{
LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
{
lnkbtnPage.Enabled = false;
lnkbtnPage.Style.Add("fone-size", "14px");
lnkbtnPage.Font.Bold = true;
}
}And at the end we have Event Handlers for the next, previous, first & last LinkButtons as in the following:
protected void lbtnNext_Click(object sender, EventArgs e)
{
CurrentPage += 1;
this.BindItemsList();
}
protected void lbtnPrevious_Click(object sender, EventArgs e)
{
CurrentPage -= 1;
this.BindItemsList();
}
protected void lbtnLast_Click(object sender, EventArgs e)
{
CurrentPage = (Convert.ToInt32(ViewState["TotalPages"]) - 1);
this.BindItemsList();
}
protected void lbtnFirst_Click(object sender, EventArgs e)
{
CurrentPage = 0;
this.BindItemsList();
}


Prasunjeet SoniPosted Jul 9, 2017, 2:50 AM EDT
Very good logic and it resolve my issue .. thanks a lot Rizwan
Osca TanPosted Nov 17, 2016, 11:09 PM EST
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 13, 2016, 8:47 PM EST
Many thanks it helps my too much :)
Upendra Pratap ShahiPosted Jun 29, 2015, 1:06 AM EDT
good one
anwar hossainPosted Jun 5, 2015, 4:54 AM EDT
awesome paging example best ..
anwar hossainPosted Jun 5, 2015, 4:06 AM EDT
Thanks for article
Anupam SinghPosted Jul 18, 2014, 8:49 PM EDT
Nice Example. it helps me.. thanx for sharing
sa mePosted Jul 8, 2014, 6:26 AM EDT
very nice thank you sir
pankaj rojalaPosted Jun 17, 2014, 9:44 AM EDT
thank you sir ...(y)
Melike MelekPosted Apr 7, 2014, 1:46 AM EDT
Thank you very very much, I searched such article for 4 days , now I found , it works :)
AleshaPosted Dec 4, 2013, 4:20 AM EST
Thanks a million.The above article was really very helpful in understanding custom paging within Datalist
behnam pasbanPosted Dec 29, 2012, 3:34 AM EST
Thanks.:)
behnam pasbanPosted Dec 29, 2012, 3:34 AM EST
Thanks.:)
sunil patelPosted Dec 14, 2012, 8:00 PM EST
Thanks..............
Duy KienPosted Nov 8, 2012, 12:44 AM EST
thank you!
Sumit Singh PanwarPosted Nov 1, 2012, 11:33 PM EDT
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, 2:07 AM EDT
Thanks for this article .
Ahmad HussainPosted Sep 21, 2012, 2:33 PM EDT
excellent work thaks for sharing
rohit jainPosted Aug 22, 2012, 11:38 AM EDT
thanks a lot,....thank u sir its really best post on paging in datalist
Guest UserPosted Aug 21, 2012, 2:18 AM EDT
thanks good one
Raman ChoudharyPosted Aug 14, 2012, 10:18 PM EDT
thank u very much sir...
arun kothiwalPosted Aug 6, 2012, 9:20 PM EDT
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, 9:06 AM EDTEdited Jul 22, 2012, 9:07 AM EDT
Great..... This is best post that i ever read about datalist paging.
mas liPosted Jun 30, 2012, 1:49 AM EDT
Great,Thanks alot
ASPNET CODERPosted Jun 20, 2012, 10:57 PM EDT
Thanks ......... :)
ali ashooriPosted Jun 1, 2012, 3:50 AM EDT
Thanks with advance... You just get me ride of a hell with your sample! :)
Kushal TambatPosted May 24, 2012, 4:20 AM EDT
Thanks, it is very useful.
piyush SinghPosted Mar 28, 2012, 4:02 AM EDT
Thank you....it works fine, just of little bit modification...... appreciable work mate's.....
database databasePosted Jan 15, 2012, 11:30 AM EST
Thank you so much for this is ... it saved me today .. thumbs up (Y)
KevinPosted Jan 1, 2012, 10:51 PM EST
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, 6:19 PM EST
i have multy more 1000 recode? how paging?
karthikPosted Nov 20, 2011, 5:37 AM EST
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, 12:26 AM EDT
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, 2:23 AM EDT
Thanks sir .......... Very good example
suhail pawanePosted Apr 28, 2011, 9:01 PM EDT
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 14, 2011, 8:13 PM EDT
hi , Rizwan Javed sir your code real perfect . very very thanks of help
Mahmoud AlgoulPosted Feb 1, 2011, 3:19 AM EST
can any one please tell me how to make it work with Database please.
sanjay guptaPosted Jan 29, 2011, 12:14 AM EST
Hats off to u.
wachidaPosted Dec 14, 2010, 5:36 PM EST
You are smart.
shashi bhushan singhPosted Nov 11, 2010, 3:05 AM EST
Well done sir thanks
nguyen van ThangPosted Nov 2, 2010, 7:32 AM EDT
Thank you very much. Great code :) . I will use in project of me
Ram KumarPosted Sep 16, 2010, 1:53 AM EDT
i am search this type of code of my website. this code is very helpful
ram kumarPosted Jul 26, 2010, 4:07 AM EDT
great i enjoyed it. thanks
shahid abbassPosted Jul 22, 2010, 1:14 AM EDT
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, 5:14 AM EDTEdited Jul 2, 2010, 5:17 AM EDT
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, 12:10 PM EDT
MUCHAS GRACIAS POR EL CODIGO, LO ESTUBE BUSCANDO POR ALGUN TIEMPO, GRACIAS NUEVAMENTE!!!...
priya saxenaPosted Apr 22, 2010, 11:00 PM EDT
thks for the article ...is this code applicable for pagination in asp.net web services
gayathri RajagopalPosted Apr 20, 2010, 5:12 PM EDT
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, 1:19 AM EST
Hey Thank u very much.... Really this code helps me lot...... Good Bless u...........
foramPosted Mar 11, 2010, 7:40 PM EST
Thanks Man.... It helps me alot....
Iman AlkafouryPosted Nov 24, 2009, 1:54 AM EST
I used your code , thanks a lot.
asd asdPosted Nov 23, 2009, 8:02 PM EST
Thanks maan.. it really made my life easy
cihangir surPosted Oct 9, 2009, 5:23 AM EDT
Hi my names cihangir.Iam from Turkey. this is beautiful project. tahnks.
priyankaPosted Sep 22, 2009, 4:06 AM EDT
thanx so much for this code..
summaiya shaikhPosted Jun 24, 2009, 4:40 AM EDT
thank you very much for this article
hhillPosted Jun 15, 2009, 1:54 AM EDT
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 7, 2009, 9:44 PM EDTEdited Jun 7, 2009, 9:52 PM EDT
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, 2:27 AM EDT
its greate help thank your
joohithaPosted Mar 6, 2009, 6:54 PM EST
Thank u very much. U know iam serching for the same code for so many days. It's Wroking fine.