Introduction
This article describes DataList Paging with Next, previous and First, Last page access functionality. A numbers list is also displayed for accessing pages faster.

Background
Few days back when I required to implement custom paging in DataList(like a google paging). I search out 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 discuss complete example code.
HTML in Default.aspx contains two DataList's
1) dListItems (Main DataList showing actual contents to implement paging)
2) dlPaging (used for displaying page numbers as navigation links).
Four LinkButton's to move next, previous, first and last page
A Label to display current page number out of total number of pages.
Now we come to .cs code. There are three get, set properties
1-CurrentPage (To keep current page index)
2-fistIndex & 3-lastIndex (These two properties are used to for paging DataList)
object objPage = ViewState["_CurrentPage"];
_CurrentPage = (int)objPage;
set { ViewState["_CurrentPage"] = value; }
if (ViewState["_FirstIndex"] == null)
_FirstIndex = Convert.ToInt32(ViewState["_FirstIndex"]);
set { ViewState["_FirstIndex"] = value; }
if (ViewState["_LastIndex"] == null)
_LastIndex = Convert.ToInt32(ViewState["_LastIndex"]);
set { ViewState["_LastIndex"] = value; }
we need a PagedDataSource object to get and set various properties for custom paging
PagedDataSource _PageDataSource = new PagedDataSource();
GetDataTable() method simply returns a DataTable. In example code i
am not pulling data from database. i have created a custom DataTable
and added 100 Rows to DataTable using 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);
for (int i = 1; i <= 100; i++)
row["title"] = "Sample Row: I am putting here sample text for row " + i;
Method BindItemsList() gets DataTable by calling GetDataTable() method
Create DataSource for _PageDataSource, set PageDataSource properties and at the end binds 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();
BindItemsList() calls doPaging() method which actually binds paging DataList.
DataTable dt = new DataTable();
dt.Columns.Add("PageIndex");
dt.Columns.Add("PageText");
fistIndex = CurrentPage - 5;
lastIndex = CurrentPage + 5;
if (lastIndex > Convert.ToInt32(ViewState["TotalPages"]))
lastIndex = Convert.ToInt32(ViewState["TotalPages"]);
fistIndex = lastIndex - 10;
for (int i = fistIndex; i < lastIndex; i++)
DataRow dr = dt.NewRow();
this.dlPaging.DataSource = dt;
this.dlPaging.DataBind();
Make a to Call BindItemList Method at PageLoad()
protected void Page_Load(object sender, EventArgs e)
dlPaging_ItemCommand sets CurrentPage Property and again makes a call to BindItemsList()
protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
if (e.CommandName.Equals("Paging"))
CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
dlPaging_ItemDataBound will set Enabled equals to false when binding the LinkButton which is currently clicked from paging list
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 next, previous, first & last LinkButton's
protected void lbtnNext_Click(object sender, EventArgs e)
protected void lbtnPrevious_Click(object sender, EventArgs e)
protected void lbtnLast_Click(object sender, EventArgs e)
CurrentPage = (Convert.ToInt32(ViewState["TotalPages"]) - 1);
protected void lbtnFirst_Click(object sender, EventArgs e)