Home ProgrammingASP.NET Pagination with DataList

Pagination with DataList

by John Bhatt
0 comment 923 views
As we know, DataList is the most versatile Data Bound Control available in ASP.NET. Where you can display data as per your need in your required style, direction etc.
But there is some problem also. There is no option to Paginate the Data Loaded in DataList directly. In case we have too many records, we can not display all them in One Page without paginating.To escape from this situation, Developers move to GridView, which has easy support for Paginating data.Today, we will learn How to do Pagination with DataList data in Simple steps.First create a DataList as per your requirement. I made like below which is quite simple.Pagination with DataList

Lets place Next and Previous Buttons for Navigation. Code for above design is below:

Code in ASPX Page (Design)


Employee Name Designation Salary Edit
<asp:TextBox ID="txtEname" Text='’ runat=”server” /> <asp:TextBox ID="txtJOb" Text='’ runat=”server” /> <asp:TextBox ID="txtSal" Text='’ runat=”server” />

Next Previous


Now Lets to to Backend. ASPX.CS Page (Code Behind):


protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
CurrentPageIndex = 0;
int pg = 0;
void showData()
PagedDataSource pgd = new PagedDataSource();
SqlDataAdapter da = new SqlDataAdapter(“select * from emp”, “Data Source=.SQLDB;Database=Test1;Integrated Security=True”);
DataSet ds = new DataSet();
da.Fill(ds);pgd.DataSource = ds.Tables[0].DefaultView;
pgd.CurrentPageIndex = CurrentPageIndex ;
pgd.AllowPaging = true;
pgd.PageSize = 5;LinkButton2.Enabled = !(pgd.IsLastPage);
LinkButton3.Enabled = !(pgd.IsFirstPage);DataList1.DataSource = pgd;


protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
DataList1.EditItemIndex = e.Item.ItemIndex;
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
DataList1.EditItemIndex = -1;
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)


public int CurrentPageIndex
if (ViewState[“pg”] == null)
return 0;
return Convert.ToInt16(ViewState[“pg”]);
ViewState[“pg”] = value;

protected void LinkButton2_Click(object sender, EventArgs e) {
protected void LinkButton3_Click(object sender, EventArgs e)


Now Let me Describe things happened in above code:Move to showData() function.

We created a new object of PagedDataSource. Paged data source as name stands clear, paginated data.

Now we wrote Query, Passed in Adapter, Filled DataSet which should be all clear, because it was Basic Task in ADO.NET. Now we gave it CurrentPageIndex. means where is page now.

For this, just look below to a class CurrentPageIndex which is returning value of CurentPageIndex =0 if page is beling Loaded. To check whether page is loaded or postback, we take help of ViewState. If ViewState[“pg”] == null, On Page load this will be null because it is just being initiated. On Postback it will pass the Int value stored in it to CurrentPageIndex.

We set Pagination in PagedDataSource true and Define Page size of 5 records per page.

Now Passed PagedDataSource object as Datasource of Datalist.

Now Linkbutton click events, We increased and Decreased value of CurrentPageIndex.

Hope this must be helpful to you. In case of Any problem, correction in code and help, feel free to comment below.

John Bhatt

Glad to Know, Free to Share…..




0 comment

Related Articles

Leave a Reply

Do NOT follow this link or you will be banned from the site!