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">
<ItemTemplate>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<img src="<%=DataBinder.Eval(Container.DataItem,"Image_name")%>" width="90" height="90">
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>

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

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


on .aspx.cs page...

binddatalist()
{
// use PageDatasource object for paging purpose..

PagedDataSource Page = new PagedDataSource();
try
{

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";
dlGallery.DataBind();
//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..
BindList();
}

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

{
CurrentPage +=1;
//"CurrentPage" is global variable that content the current page index..
BindList();
}

Now your paging will be solve...

regards
varun bansal


Comments

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

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

dlGallery=?
objPage=?
BindList()?
Where are the defination of there variable and functions, please attach working code if possible

Author: Padmaja11 Dec 2009 Member Level: Bronze   Points : 0

Thanks a lot because today this code helped me a lot

Author: Tim Edmonson30 Aug 2010 Member Level: Bronze   Points : 2

Thank you for this information! I looked through many options for paging of a DataList and they all seemed like poor work-arounds. This option using the PagedDataSource by far seems like the cleanest and most efficient option.

Hopefully a near future version of .NET includes paged datalist options as a default. Certainly seems like a common feature that people would be in need of.

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:
    Email: