Those who code ASP.Net web pages will probably already know, the DataGrid Server Control has built-in paging features. This means that to implement paging in a DataGrid control, all you need to do is set a couple of the DataGrids properties. This is quite easily done in Visual Studio.Net (vs.net) 2003. Go to the design screen, click on DataGrid and drag it to the page, right click the DataGrid controls and choose property builder. From there, you can easily set the pagination properties. Easy.. Can this be done too, for a DataList or Repeater controls??
Not really. For DataList, the property builder has no pagination options and for Repeater, you don’t even have a property builder. All codes have to be written in the code view. So how do you actually paginate them? Was googling it up and realised that the PagedDataSource Class is actually used to provide the DataGrids paging features and this Class can be used directly to facilitate paging on any type of DataBound control. Awesome…
Below is how you can do this..
1) Get the data that you want to page through. This can be an array, a DataSet, a DataReader, or any other object that can be assigned to a data Web control’s DataSource property.
2)Create the PagedDataSource instance, and assign the data to page through to the PagedDataSource’s DataSource property.
3) Set the PagedDataSource class’s paging properties, such as setting AllowPaging to True, and setting PageSize (to indicate how many records per page to show).
4) Assign the PagedDataSource instance to the data Web control’s DataSource property and call the data Web control’s DataBind() method and Voila! Instant pagination support for a Repeater. Its not too hard to understand.
// initialise all the page variables
int curr_page;
// setting the current page and the record to start from
if (Request.QueryString["page"] == null
|| int.Parse(Request.QueryString["page"]) < 1)
{ tcurr_page = 1; }
else
{ curr_page = int.Parse(Request.QueryString["page"]); }
//All your initialisation of the stored procedure goes here
// Setting a new DataAdapter to use the GetBooks
// stored procedures
SqlDataAdapter myAdapter = new SqlDataAdapter(GetBooks);
// Use a DataSet - required for paging in a PagedDataSource
// object and populating it with data from the DataAdapter
DataSet ds = new DataSet();
myAdapter.Fill(ds);
// Populate the PagedDataSource with the DataSet ds
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables[0].DefaultView;
// Indicate that the data should be paged
pds.AllowPaging = true;
// Set the number of items you wish to display per page
pds.PageSize = 3;
// Set the PagedDataSource's current page
pds.CurrentPageIndex = curr_page - 1;
// Binding the PagedDataSource to the BookList DataRepeater
BookList.DataSource = objPds;
BookList.DataBind();
Cool thing bout the PagedDataSource Class is that you can make use of the available methods such as PageCount(), IsFirstPage(), IsLastPage() etc…. Makes your life easier rather than to manually calculate if the current page you are in is the last or first or how many records there are in the Repeater