@*
* FILE : /SiteElements/Razorviews/MyListingRazorview.cshtml
*@
@{
@*
* NOTES
* *****
* To call html helpers we refer to the file name and helper name:
*
* Example : FileName.HelperName();
*@
// query-string parameters
int QS_PAGE = Request.QueryString["page"].AsInt() > 0 ? Request.QueryString["page"].AsInt() : 1;
// calculated parameters
int NUMBER_OF_PAGES = 20; // this would normally be calculated by using the result count and a formula (below), but is hard-coded for this example
// formula
NUMBER_OF_PAGES = NUMBER_OF_RESULTS / DISPLAY;
if(NUMBER_OF_RESULTS % DISPLAY != 0)
{
NUMBER_OF_PAGES = NUMBER_OF_PAGES + 1;
}
START_INDEX = (QS_PAGE * DISPLAY) - DISPLAY;
@HtmlHelpers.RenderPagination(QS_PAGE, NUMBER_OF_PAGES)
}
@*
* FILE : /App_Code/HtmlHelpers.cshtml
*@
@using System.Collections.Specialized
@helper RenderPagination(int pageNumber, int pageCount)
{
@*
* HELPER : RENDER PAGINATION
* **************************
* Description : takes an input 'current page number' and 'page count' to
* work at what point in the pagination we are at and the
* pagination size
*
* Example use : a site that has multiple custom listing razorviews (search,
* news, events etc) that all shared the same html structure
* for pagination and query-string parameters
*@
<ul>
@{
// previous link
if(pageNumber > 1)
{
// url
NameValueCollection qs = HttpUtility.ParseQueryString(Request.QueryString.ToString());
qs.Set("page", (pageNumber - 1).ToString());
string url = Request.Url.AbsolutePath + "?" + qs.ToString();
<li class="previous">
<a href="@url" title="Previous page">‹</a>
</li>
}
// page links
for(int i = 1; i <= pageCount; i++)
{
int rangeMin = pageNumber - 4;
int rangeMax = pageNumber + 4;
if(i >= rangeMin && i <= rangeMax)
{
// css
string css = (i == pageNumber) ? "selected" : null;
// url
NameValueCollection qs = HttpUtility.ParseQueryString(Request.QueryString.ToString());
qs.Set("page", i.ToString());
string url = Request.Url.AbsolutePath + "?" + qs.ToString();
<li class="@css">
<a href="@url">@i</a>
</li>
}
}
// next link
if (pageNumber < pageCount)
{
// url
NameValueCollection qs = HttpUtility.ParseQueryString(Request.QueryString.ToString());
qs.Set("page", (pageNumber + 1).ToString());
string url = Request.Url.AbsolutePath + "?" + qs.ToString();
<li class="next">
<a href="@url" title="Next page">›</a>
</li>
}
}
</ul>
}