I was tasked to build an rss reader the other day and created a Datalist to display the output.
I wanted to display page numbers but then realised that the datalist doesn't have any built in functionality for paging (like the DataGrid and GridView).
I was faced with the prospect of rolling my own until I realised that most of the work had already been done for me with the PagedDataSource class! This neat class gives me all of the same functionality as native paging, and was easy to setup. It includes all of the important ingredients to do my own paging such as properies for.....PageSize, PageCount, IsFirstpage, IsLastPage etc. Here is a chunk of my code....
PagedDataSource pagedItems = new PagedDataSource();
pagedItems.DataSource = myArrayList; //the datasource
pagedItems.AllowPaging = true;
pagedItems.PageSize = maxItems;
try
{
pagedItems.CurrentPageIndex = Int32.Parse(Request.QueryString["page"]);
}
catch (Exception ex)
{
pagedItems.CurrentPageIndex = 0;
}
lblPages.Text = "";
if (pagedItems.PageCount > 1)
{
for (int x = 1; x < pagedItems.PageCount + 1; x++)
{
lblPages.Text += "
+ "page=" + (x - 1) + ">" + x.ToString() + "";
}
}