How to: Create pages (pagination) for your custom data set (in ASP.NET)

I needed this while building a custom Search facility for a customer. I was working with the Sitefinity API and the mechanism should work with documents that were hosted on the Sitefinity CMS.

Notice that in this case, we’re talking for an ASP.NET solution, which would also need a way to keep track of where we are in our list of results, since there’s no straightforward way of storing states in ASP.

It might at first seem daunting, but in reality, it’s just too easy to build a paginated dataset. The framework already provides us with the PagedDataSource which will make most of the work.

You will need to define a new PagedDataSource

PagedDataSource pgSortedResults = new PagedDataSource();

bind it to a datasource (mine was a DataTable)”:

pgSortedResults.DataSource = dtSortedResults.AsDataView();

(notice that the DataSource needs to implement the IEnumerable. That’s why we use the .AsDataView(). Using the DataTable would not compile).

Set up the paging directives (I was using a drop down list to get the size of my pages. You can just use an integer value instead):

pgSortedResults.AllowPaging = true;
pgSortedResults.PageSize = Convert.ToInt32(ddlPager.SelectedValue);

And the tricky bit, as was expected, is to keep track of pages, previous, next and current.

pgSortedResults.CurrentPageIndex = CurrentPage();

(scroll down for the method)

if (pgSortedResults.CurrentPageIndex < 0 || pgSortedResults.CurrentPageIndex >pgSortedResults.PageCount)
pgSortedResults.CurrentPageIndex = 0;

(this is a funny bit, yes, but just in case you mishandle the pagination, it will keep your code from going haywire by pointing the page back to the first one)

if (pgSortedResults.IsFirstPage)

//code to disable/delete the button that navigates to the previous page of results


if (pgSortedResults.IsLastPage)

//same as above. code to disable/delete the button that navigates to the next page

Don’t forget that if you have a “Current Page: “ label, you will need to update the page number for that.

lblControl.Text = Convert.ToString(pgSortedResults.CurrentPageIndex + 1) + “/” + pgSortedResults.PageCount.ToString();

The above will give you a “1/5” text, meaning that you’re on page 1 of 5.

As for the CurrentPage() method:

protected int CurrentPage()
Session[“DisplayPager”] = 1;
if (Session[“_CurrentPage”] == null)
Session[“_CurrentPage”] = 0;
return 0;
return Convert.ToInt32(Session[“_CurrentPage”]);

You would have to go with session variables to keep track of where you are in your search result pages.

Lastly, you will need to provide the code for your previous and next results. Although this is probably not the best way to do it, it worked for me and the search results were calculated in no considerable time. This allowed me to use this approach. What happens here is that you will set up your method that creates the original data set (your search results prior to pagination) to re-run. Before binding the values to your output, you will move a page forwards or backwards. ie:

protected void btn_NextPage(Object sender, EventArgs e)
int currentPage = Convert.ToInt32(Session[“_CurrentPage”].ToString()); //get the current page from the session variable
Session[“_CurrentPage”] = currentPage + 1; //add one to the current page
//code that creates the original dataset
} //do the same for your PreviousPage button

Depending on how you’ve coded your solution, you will need to pass your new dataset to the pagination mechanism you created earlier on. In my case, I bind the pager to the results in the same method where I build the dataset, so, it’s OK for me to consider my code complete at that point.

MGR: the Intelogist

About MGR: the Intelogist

SharePoint Server developer, turned Sitefinity developer, turned Angular developer, turned SharePoint Online consultant, turned Unily consultant, turned O365 consultant... Never a dull moment!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

This site uses Akismet to reduce spam. Learn how your comment data is processed.