Click here to Skip to main content
15,888,527 members
Articles / Web Development / ASP.NET
Article

Paging using PagedDataSource class

Rate me:
Please Sign up or sign in to vote.
3.70/5 (20 votes)
18 Sep 20061 min read 122.5K   46   29
Paging using PagedDataSource class for Repeater or Datalist

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()

{
<P>//Creating an object for the 'PagedDataSource' for holding the data.  </P>
<P>PagedDataSource objPage = new PagedDataSource();</P><P>
try
{
 
 DataSet ds = ....\\DataSource.
    
 objPage.AllowPaging = true;
     
 //Assigning the datasource to the 'objPage' object.
 objPage.DataSource = ds.Tables["Gallery"].DefaultView;</P>
<P> //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();
<P>}
    
catch(Exception ex)
{
 throw ex;
}
<P>}</P>

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.

<P>//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;
<P>   </P>

Thats all. So here is the complete BindList function.

public void BindList()

{
<P>//Creating an object for the 'PagedDataSource' for holding the data. 
 
PagedDataSource objPage = new PagedDataSource();
   
<P>try
{
 
 DataSet ds = ....\\DataSource.
    
 objPage.AllowPaging = true;
     
 //Assigning the datasource to the 'objPage' object.
 objPage.DataSource = ds.Tables["Gallery"].DefaultView;
<P> //Setting the Pagesize
 objPage.PageSize = 8;
     
 //"CurrentPage" is public static variable to hold the current page index value declared in the global section. </P><P> objPage.CurrentPageIndex = CurrentPage;</P>
<P> //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();
<P>}
    
catch(Exception ex)
{
 throw ex;
}
<P>}</P>

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


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 3 Pin
Divyanshu Kumar20-Dec-15 2:32
Divyanshu Kumar20-Dec-15 2:32 
QuestionCorrections Pin
dineshkumarw18-Dec-14 23:52
professionaldineshkumarw18-Dec-14 23:52 
QuestionGood Job ! Pin
johann678801-Nov-13 5:44
johann678801-Nov-13 5:44 
GeneralGood article Pin
Rohit_kakria3-Feb-11 1:07
Rohit_kakria3-Feb-11 1:07 
GeneralMy vote of 1 Pin
Devinmccloud27-Nov-10 21:14
Devinmccloud27-Nov-10 21:14 
GeneralMy vote of 2 Pin
nishant.maurya2-Nov-10 20:09
nishant.maurya2-Nov-10 20:09 
QuestionHow to use custom paging with paged datasource Pin
winheart16-Dec-09 22:27
winheart16-Dec-09 22:27 
GeneralCurrentPage" is public static variable to hold the current page index value declared in the global section Pin
asmani20093-Nov-09 0:58
asmani20093-Nov-09 0:58 
GeneralMy vote of 1 Pin
Arinte18-May-09 2:57
Arinte18-May-09 2:57 
Generaldont understand DataSet ds = ....\\DataSource Pin
snikos11128-Jan-09 23:36
snikos11128-Jan-09 23:36 
Generaldatalist not populating Pin
Rajafusionware6-Jan-09 22:16
Rajafusionware6-Jan-09 22:16 
GeneralGood one Pin
Haresh Ambaliya24-Dec-08 0:57
Haresh Ambaliya24-Dec-08 0:57 
GeneralGood Article - sorry its from 4guysFromRolla ... Pin
junkpraveen8-May-08 22:47
junkpraveen8-May-08 22:47 
GeneralRe: Good Article - sorry its from 4guysFromRolla ... Pin
Arinte18-May-09 2:56
Arinte18-May-09 2:56 
GeneralProblem with database connection with my database ! Need Help Pin
Tyrone Boon21-Mar-08 7:54
Tyrone Boon21-Mar-08 7:54 
GeneralDoesnt Bind PagedDataSource Object Pin
vijendraAver12312-Mar-08 1:46
vijendraAver12312-Mar-08 1:46 
GeneralRe: Doesnt Bind PagedDataSource Object Pin
Sreejith Thathanattu12-Mar-08 23:29
Sreejith Thathanattu12-Mar-08 23:29 
Questionnice works but what about ?? Pin
Amr M. K.28-Oct-07 2:32
Amr M. K.28-Oct-07 2:32 
AnswerRe: nice works but what about ?? Pin
williaml30-Mar-08 12:36
williaml30-Mar-08 12:36 
That's a good point. You could always Cache the DataSource and bind to that cached data source.

Eg:

private DataView GalleryData()
{
  get {
   if (Cache["GalleryData"] != null)
      return Cache["GalleryData"] as DataView;
    else {
      DataSet dsGallery = businessLayer.GetGalleryData();
      // Insert Data into Cache with an appropriate expiry.
      // You could also have add in a Caching dependency if appropriate.
      Cache.Insert("GalleryData", dsGallery.DefaultView, null, DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration);
      return dsGallery.DefaultView;
    }
  }
}

Obviously there's many other ways to implement caching in ASP.NET, the above is just a quick simple example.

Here's another sample by Nikhil Kotari: http://www.nikhilk.net/DataSourceControlsCache.aspx[^]
GeneralVery Very Nice ! Pin
Haitham Khedre12-Sep-07 5:41
Haitham Khedre12-Sep-07 5:41 
GeneralGreat Tip! Pin
codelacky9921-Jun-07 10:10
codelacky9921-Jun-07 10:10 
GeneralArticle that the geeks in Microsoft should read! Pin
taminha19-Mar-07 1:42
taminha19-Mar-07 1:42 
GeneralFantastic article Pin
netbeaz20-Feb-07 12:43
netbeaz20-Feb-07 12:43 
Questiondatalist paging Pin
vipul.excel14-Nov-06 5:22
vipul.excel14-Nov-06 5:22 
GeneralVery good article! Pin
jonas_berg5-Oct-06 4:30
jonas_berg5-Oct-06 4:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.