Archive

ASP.NET MVC paging (digg-style)

In ASP.NET MVC there are not rich user controls, like we were used to in web forms, so things like the lack of paging grid may be a surprise to some. For a code control freek like me, this is exactly what I always wanted, so now finally I can create controls the way I want :)

Here is an HTML helper-style static function that will return exactly the same type of paging you see on this blog:

public static string links(int intCurrentPage, int intPerPage, int intNumberofItems, string pageNumberPrefix, string previousText, string nextText)
{

string strPreviousText = previousText;
string strNextText = nextText;

StringBuilder sb = new StringBuilder();

string strPageFileName = pageNumberPrefix; //adds prefix before the paging (useful for ajax by adding '#')

if (intCurrentPage < 1)
{
intCurrentPage = 1;
}

int number_of_pages = (intNumberofItems / (intPerPage + 1));

int i = 0;

//hide paging if only one page
if (number_of_pages >= 1)
{
sb.Append("<div class='clearfix paging'>");
//previous record
if (!(intCurrentPage == 1))
{
sb.Append(" <a href='" + strPageFileName.ToString() + (intCurrentPage - 1).ToString() + "' class='p'>« " + strPreviousText + "</a>");
}

if (number_of_pages < 20)
{
// if there are less than 20 record no paging breaking

//numbers
for (i = 0; i <= number_of_pages; i++)
{
if (!(i == intCurrentPage - 1))
{
sb.Append(" <a href='" + strPageFileName + (i + 1) + "'> " + (i + 1) + " </a>");
}
else
{
sb.Append("<span>" + (i + 1) + " </span>");
}

}
}
else
{

if (intCurrentPage <= 10)
{

//numbers
for (i = 0; i <= 10; i++)
{
if (!(i == intCurrentPage - 1))
{
sb.Append(" <a href='" + strPageFileName + (i + 1) + "'> " + (i + 1) + " </a>");
}
else
{
sb.Append("<span>" + (i + 1) + " </span>");
}
}

sb.Append("<span class='pg_dots'>...</span>");

//numbers
for (i = number_of_pages - 2; i <= number_of_pages; i++)
{
if (!(i == intCurrentPage - 1))
{
sb.Append(" <a href='" + strPageFileName + (i + 1) + "'> " + (i + 1) + " </a>");
}
else
{
sb.Append("<span>" + (i + 1) + " </span>");
}

}
}

else if (intCurrentPage >= number_of_pages - 8)
{
//last 10 numbers scenerio
//numbers
for (i = 0; i <= 3; i++)
{
if (!(i == intCurrentPage - 1))
{
sb.Append(" <a href='" + strPageFileName + (i + 1) + "'> " + (i + 1) + " </a>");
}
else
{
sb.Append("<span>" + (i + 1) + " </span>");
}
}

sb.Append("<span class='pg_dots'>...</span>");

//numbers
for (i = number_of_pages - 10; i <= number_of_pages; i++)
{
if (!(i == intCurrentPage - 1))
{
sb.Append(" <a href='" + strPageFileName + (i + 1) + "'> " + (i + 1) + " </a>");
}
else
{
sb.Append("<span>" + (i + 1) + " </span>");
}
}
}
else
{
//numbers
for (i = 0; i <= 3; i++)
{
if (!(i == intCurrentPage - 1))
{
sb.Append(" <a href='" + strPageFileName + (i + 1) + "'> " + (i + 1) + " </a>");
}
else
{
sb.Append("<span>" + (i + 1) + " </span>");
}
}

sb.Append("<span class='pg_dots'>...</span>");

//numbers
for (i = intCurrentPage - 5; i <= intCurrentPage + 3; i++)
{
if (!(i == intCurrentPage - 1))
{
sb.Append(" <a href='" + strPageFileName + (i + 1) + "'> " + (i + 1) + " </a>");
}
else
{
sb.Append("<span>" + (i + 1) + " </span>");
}
}

sb.Append("<span class='pg_dots'>...</span>");

//numbers
for (i = number_of_pages - 3; i <= number_of_pages; i++)
{
if (!(i == intCurrentPage - 1))
{
sb.Append(" <a href='" + strPageFileName + (i + 1) + "'> " + (i + 1) + " </a>");
}
else
{
sb.Append("<span>" + (i + 1) + " </span>");
}

}
}
}

//next record
if (!(intCurrentPage == number_of_pages + 1))
{
sb.Append(" <a href='" + strPageFileName + (intCurrentPage + 1) + "' class='n'>" + strNextText + " »</a>");
}
sb.Append("</div><!-- end of 'paging' -->");

//builds string
return sb.ToString();
}
else
{
return "";

}
}

Comments: