Paging of Data list

When you have datalist there is one main problem in it and that is paging.. because paging is not allowed in data list.. You can solve this problem using this method...

On .aspx page.
we have create datalist with images.

<asp:DataList runat="server" id="datalist1" RepeatColumns="4" RepeatDirection="Horizontal">
<table border="0" cellpadding="0" cellspacing="0">
<img src="<%=DataBinder.Eval(Container.DataItem,"Image_name")%>" width="90" height="90">

we have take two linkbutton for next and previous button for using paging..

<table border="0" width="410">
<td align="left">
<asp:LinkButton ID="lbPrev" Runat="server">
<td align="right">
<asp:LinkButton ID="lbNext" Runat="server">

on .aspx.cs page...

// use PageDatasource object for paging purpose..

PagedDataSource Page = new PagedDataSource();

DataSet ds = ds // ds is your datasource..
Page.AllowPaging = true;

//page object store the datasource of datalist..
Page.DataSource = ds.Tables[0].DefaultView;
Page.PageSize = 8;

//"CurrentPage" is global variable that content the current page index..
Page.CurrentPageIndex = CurrentPage;

dlGallery.DataSource = Page;
dlGallery.DataKeyField = "Image_ID";
//now datalist only show the a images...
//due to "Page.PageSize = 8;" statement...

//visible true or false of next and previous button
//according to last and first page...
lbtnNext.Enabled = !objPage.IsLastPage;
lbtnPrev.Enabled = !objPage.IsFirstPage;

catch(Exception ex)
throw ex;


then we have maintain paging using link and next button as below...

private void lbPrev_Click(object sender, System.EventArgs e)

CurrentPage -=1;
//"CurrentPage" is global variable that content the current page index..

private void lbNext_Click(object sender, System.EventArgs e)

CurrentPage +=1;
//"CurrentPage" is global variable that content the current page index..

Now your paging will be solve...

varun bansal


Author: Nisar21 Apr 2009 Member Level: Gold   Points : 1

Hey i want to use your code for paging but i few thing are missing

Where are the defination of there variable and functions, please attach working code if possible

Author: Pal (Parthiv) Patel31 Aug 2010 Member Level: Gold   Points : 1

hey varun

this one is awesome code snippet

keep it up

put good codes and earn money and points

give your presentation in good way.

Guest Author: Amresh Bahadur Singh08 May 2013

First Thanks a lot because today this code helped me......

dlGallery=?.............replace to datalist1
objPage=?.............. replace to page
BindList()?............. replace to binddatalist()

Guest Author: sanila13 Jun 2013

could you help... ow do the pagination in 10 images maximum in page

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: