Click here to Skip to main content
6,594,088 members and growing! (16,535 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

Paging using PagedDataSource class

By Sreejith Thathanattu

Paging using PagedDataSource class for Repeater or Datalist
C# 1.0, Windows, .NET, ASP.NET, Visual Studio, Dev
Posted:18 Sep 2006
Views:47,641
Bookmarked:38 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
12 votes for this article.
Popularity: 4.00 Rating: 3.71 out of 5
3 votes, 25.0%
1
1 vote, 8.3%
2

3
2 votes, 16.7%
4
6 votes, 50.0%
5

Introduction

When we want to display data in Datalist or repeater we often have one problem PAGING. Datalist and Repeater both are powerful controls to customize "How the data is displayed in page".

DataList and Repeater both doesn't have auto paging support like the datagrid. Usually we diffent kind of custom paging to overcome this issue.

But there is a class in the .NET Framework that we can use to provide datagrid like paging support to datalist and repeater.

Here i will explain how to use the "PagedDataSource class" (System.Web.UI.WebControls).

In this example i will use a datalist to create an image gallery with paging support.

So lets create the datalist,

<asp:DataList runat="server" 
id="dlGallery" RepeatColumns="4" 
RepeatDirection="Horizontal">
<ItemTemplate>
<table border="0" 
cellpadding="0" 
cellspacing="0">
  <tr>
       <td><img  src="<%#DataBinder.Eval(Container.DataItem,"Image_URL")%>"  
width="90" 
height="90">
   </td>
  </tr>
         
</table>
</ItemTemplate>
</asp:DataList>

For the paging create two linkbuttons,

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

<td align="right"><asp:LinkButton ID="lbtnNext" 
Runat="server">Next</asp:LinkButton> 
</td>
</tr>
</table>

         


Now the html part is over now we have to bind the data to the datalist.
For that lets create a public function "BindList()".

public void BindList()

{

//Creating an object for the 'PagedDataSource' for holding the data.

PagedDataSource objPage = new PagedDataSource();

try { DataSet ds = ....\\DataSource. objPage.AllowPaging = true; //Assigning the datasource to the 'objPage' object. objPage.DataSource = ds.Tables["Gallery"].DefaultView;

//Setting the Pagesize objPage.PageSize = 8; //"CurrentPage" is public static variable to hold the current page index value declared in the global section. objPage.CurrentPageIndex = CurrentPage; //Assigning Datasource to the DataList. dlGallery.DataSource = objPage; dlGallery.DataKeyField = "Image_ID"; dlGallery.DataBind();

} catch(Exception ex) { throw ex; }

}

Now the datalist will display the first 8 records ( because i set "objPage.PageSize = 8").

For navigation we have to add littile bit code to the event handlers.  

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

{
 CurrentPage -=1;
 BindList();
}

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

{
 CurrentPage +=1;
 BindList();
}

See we only need to +/- the value of the "CurrentPage " to provide navigation.

If u want enable/disable the navigation links according to the first/last page then add this few lines after the " dlGallery.DataBind();" line.

//Checking for enabling/disabling next/prev buttons. //Next/prev buton will be disabled when is the last/first page of the pageobject. lbtnNext.Enabled = !objPage.IsLastPage; lbtnPrev.Enabled = !objPage.IsFirstPage;

Thats all. So here is the complete BindList function.

public void BindList()

{

//Creating an object for the 'PagedDataSource' for holding the data. PagedDataSource objPage = new PagedDataSource();

try { DataSet ds = ....\\DataSource. objPage.AllowPaging = true; //Assigning the datasource to the 'objPage' object. objPage.DataSource = ds.Tables["Gallery"].DefaultView;

//Setting the Pagesize objPage.PageSize = 8; //"CurrentPage" is public static variable to hold the current page index value declared in the global section.

objPage.CurrentPageIndex = CurrentPage;

//Checking for enabling/disabling next/prev buttons. //Next/prev buton will be disabled when is the last/first page of the pageobject. lbtnNext.Enabled = !objPage.IsLastPage; lbtnPrev.Enabled = !objPage.IsFirstPage; //Assigning Datasource to the DataList. dlGallery.DataSource = objPage; dlGallery.DataKeyField = "Image_ID"; dlGallery.DataBind();

} catch(Exception ex) { throw ex; }

}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Sreejith Thathanattu


Member

Occupation: Software Developer
Location: India India

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 22 of 22 (Total in Forum: 22) (Refresh)FirstPrevNext
GeneralCurrentPage" is public static variable to hold the current page index value declared in the global section Pinmemberasmani20091:58 3 Nov '09  
GeneralMy vote of 1 PinmemberArinte3:57 18 May '09  
Generaldont understand DataSet ds = ....\\DataSource Pinmembersnikos1110:36 29 Jan '09  
Generaldatalist not populating PinmemberRajafusionware23:16 6 Jan '09  
GeneralGood one PinmemberHaresh Ambaliya1:57 24 Dec '08  
GeneralGood Article - sorry its from 4guysFromRolla ... Pinmemberjunkpraveen23:47 8 May '08  
GeneralRe: Good Article - sorry its from 4guysFromRolla ... PinmemberArinte3:56 18 May '09  
GeneralProblem with database connection with my database ! Need Help PinmemberTyrone Boon8:54 21 Mar '08  
GeneralDoesnt Bind PagedDataSource Object PinmembervijendraAver1232:46 12 Mar '08  
GeneralRe: Doesnt Bind PagedDataSource Object PinmemberSreejith Thathanattu0:29 13 Mar '08  
Generalnice works but what about ?? PinmemberAmr Al-khatib3:32 28 Oct '07  
GeneralRe: nice works but what about ?? Pinmemberwilliaml13:36 30 Mar '08  
GeneralVery Very Nice ! Pinmemberhaitham hamed housin6:41 12 Sep '07  
GeneralGreat Tip! Pinmembercodelacky9911:10 21 Jun '07  
GeneralArticle that the geeks in Microsoft should read! Pinmembertaminha2:42 19 Mar '07  
GeneralFantastic article Pinmembernetbeaz13:43 20 Feb '07  
Questiondatalist paging Pinmembervipul.excel6:22 14 Nov '06  
GeneralVery good article! Pinmemberjonas_berg5:30 5 Oct '06  
GeneralRe: Very good article! PinmemberSreejith Thathanattu0:47 23 Oct '06  
GeneralRe: Very good article! PinmemberChris Cameron9:54 29 Nov '06  
GeneralNice article Pinmembervik2020:51 26 Sep '06  
GeneralRe: Nice article PinmemberSreejith Thathanattu0:48 23 Oct '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 Sep 2006
Editor:
Copyright 2006 by Sreejith Thathanattu
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project