Monday, September 19, 2005

Paging using PagedDataSource class

PagedDataSource objPagedDataSource = new PagedDataSource();
objPagedDataSource.DataSource = //All rows - some datasource (like DataTable, DataView, DataSet's Dtatable, DataReader, Array etc.)

objPagedDataSource.AllowPaging = true;
objPagedDataSource.CurrentPageIndex = 0;
objPagedDataSource.PageSize = 5;

repeater.DataSource = objPagedDataSource;
repeater.DataBind();


Now you create your own navigation buttons First, Prev, Next, Last.
In their server side event handlers, you update bind the repeater with other set of rows as follows


PagedDataSource objPagedDataSource = new PagedDataSource();
objPagedDataSource.DataSource = //ALL rows - some datasource (like DataTable, DataView, DataSet's Dtatable, DataReader, Array etc.)

objPagedDataSource.AllowPaging = true;
objPagedDataSource.CurrentPageIndex = //new value depending on which navigational button is clicked
objPagedDataSource.PageSize = 5;

repeater.DataSource = objPagedDataSource;
repeater.DataBind();


When AllowCustomPaging is set to true, PagedDataSource looks at the total record count of the DataSource, PageSize and CurrentPageIndex properties to set its IsFirstPage, IsLastPage and other properties.

When the repeater's DataBind method is called, it accesses the objPagedDataSource's DataSource, CurrentPageIndex and PageSize properties and loads itself with the appropriate set of rows.
Here All of the rows are being accessed from the database.
So, if this data is stored in the viewstate, then the size of the file sent back and forth the browser becomes very large and is unnecessary.

You can hide/show the navigational buttons depending on the PagedDataSource class's IsLastPage, IsFirstPage properties.

CUSTOM PAGING



PagedDataSource objPagedDataSource = new PagedDataSource();
objPagedDataSource.DataSource = //Only first set of rows - some datasource (like DataTable, DataView, DataSet's Dtatable, DataReader, Array etc.)

objPagedDataSource.AllowPaging = true;
objPagedDataSource.AllowCustomPaging = true;
objPagedDataSource.VirtualCount = //TOTAL number of rows
objPagedDataSource.CurrentPageIndex = 0;
objPagedDataSource.PageSize = 5;

repeater.DataSource = objPagedDataSource;
repeater.DataBind();


Now you create your own navigation buttons First, Prev, Next, Last.
In their server side event handlers, you update bing the repeater with other set of rows as follows

PagedDataSource objPagedDataSource = new PagedDataSource();
objPagedDataSource.DataSource = //only the required set of rows - some datasource (like DataTable, DataView, DataSet's Datatable, DataReader, Array etc.)

objPagedDataSource.AllowPaging = true;
objPagedDataSource.AllowCustomPaging = true;
objPagedDataSource.VirtualCount = //TOTAL number of rows
objPagedDataSource.CurrentPageIndex = //new value depending on which navigational button is clicked
objPagedDataSource.PageSize = 5;

repeater.DataSource = objPagedDataSource;
repeater.DataBind();

When AllowCustomPaging is set to true, PagedDataSource looks at the VirtualCount, PageSize and CurrentPageIndex properties to set its IsFirstPage, IsLastPage and other properties.

When the repeater's DataBind method is called, it accesses the objPagedDataSource's DataSource, CurrentPageIndex and PageSize properties and loads itself with the appropriate set of rows.
Here only the required set of the rows are accessed from the database for each page.
So, if this data is stored in the viewstate, then the size of the file sent back and forth the browser is minimized.

You can hide/show the navigational buttons depending on the PagedDataSource class's IsLastPage, IsFirstPage properties.

0 Comments:

Post a Comment

<< Home