当网站中的内容较多时,常常需要用到分页功能来实现良好的用户体验。下面就是C#页码导航的实现。
一、分页算法
如何实现分页?我们需要首先确定一页要显示的记录数(pageSize)和当前页码(currentPageNum),其次需要得到数据表中数据总数(totalCount),然后根据这三个值来计算总的页数(totalPageNum)。
totalPageNum = Math.Ceiling(totalCount / (double)pageSize);
同时,需要考虑到特殊情况,如总记录数为0,或者不足一页时等。
二、C#页码导航代码实现
示例一:实现简单的页码导航功能,代码如下:
public static string GetPageNav(int totalCount, int currentPageNum, int pageSize, string url)
{
StringBuilder pageNavHtml = new StringBuilder();
int totalPageNum = (int)Math.Ceiling(totalCount / (double)pageSize);
if (totalPageNum > 1)
{
pageNavHtml.Append("<div class=\"pagination\">");
if (currentPageNum > 1)
{
pageNavHtml.Append("<a href=\"" + url + "&page=" + (currentPageNum - 1) + "\">上一页</a>");
}
for (int i = 1; i <= totalPageNum; i++)
{
if (i == currentPageNum)
{
pageNavHtml.Append("<span class=\"current_page\">" + i + "</span>");
}
else
{
pageNavHtml.Append("<a href=\"" + url + "&page=" + i + "\">" + i + "</a>");
}
}
if (currentPageNum < totalPageNum)
{
pageNavHtml.Append("<a href=\"" + url + "&page=" + (currentPageNum + 1) + "\">下一页</a>");
}
pageNavHtml.Append("</div>");
}
return pageNavHtml.ToString();
}
该代码实现了一个简单的分页导航功能,展示形式如下:
上一页 1 2 3 4 5 下一页
示例二:提供了更多的自定义参数,更灵活的用法,代码如下:
public static string GetPageNavCustom(int totalCount, int currentPageNum, int pageSize, string url, string prevText, string nextText, string ellipseText, string currentClass, string pageClass)
{
StringBuilder pageNavHtml = new StringBuilder();
int totalPageNum = (int)Math.Ceiling(totalCount / (double)pageSize);
if (totalPageNum > 1)
{
pageNavHtml.Append("<div class=\"pagination\">");
if (currentPageNum > 1)
{
pageNavHtml.Append("<a href=\"" + url + "&page=" + (currentPageNum - 1) + "\">" + prevText + "</a>");
}
if (totalPageNum <= 7)
{
for (int i = 1; i <= totalPageNum; i++)
{
if (i == currentPageNum)
{
pageNavHtml.Append("<span class=\"" + currentClass + "\">" + i + "</span>");
}
else
{
pageNavHtml.Append("<a class=\"" + pageClass + "\" href=\"" + url + "&page=" + i + "\">" + i + "</a>");
}
}
}
else
{
int half = 3;
int start = Math.Max(1, currentPageNum - half);
int end = Math.Min(start + 6, totalPageNum);
if (start > 1)
{
pageNavHtml.Append("<a class=\"" + pageClass + "\" href=\"" + url + "&page=1\">1</a>");
pageNavHtml.Append("<span class=\"ellipse\">" + ellipseText + "</span>");
}
for (int i = start; i <= end; i++)
{
if (i == currentPageNum)
{
pageNavHtml.Append("<span class=\"" + currentClass + "\">" + i + "</span>");
}
else
{
pageNavHtml.Append("<a class=\"" + pageClass + "\" href=\"" + url + "&page=" + i + "\">" + i + "</a>");
}
}
if (end < totalPageNum)
{
pageNavHtml.Append("<span class=\"ellipse\">" + ellipseText + "</span>");
pageNavHtml.Append("<a class=\"" + pageClass + "\" href=\"" + url + "&page=" + totalPageNum + "\">" + totalPageNum + "</a>");
}
}
if (currentPageNum < totalPageNum)
{
pageNavHtml.Append("<a href=\"" + url + "&page=" + (currentPageNum + 1) + "\">" + nextText + "</a>");
}
pageNavHtml.Append("</div>");
}
return pageNavHtml.ToString();
}
该代码实现了一个更加灵活自定义的分页导航功能,展示形式如下:
上一页 1 2 3 4 … 8 下一页
三、总结
以上就是C#页码导航的实现攻略。我们需要首先明确分页算法以及总页数,然后根据自身需求实现自定义的页码导航功能。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C#页码导航显示及算法实现代码 - Python技术站