【发布时间】:2013-01-07 19:31:33
【问题描述】:
我是新来的
我和here中的马库斯有同样的问题
我正在尝试执行的分页应该如下所示:
1 2 3 4 5 6 ... 101
当我点击数字 5 时,我希望它显示如下数字:
1 ... 3 4 5 6 7 ... 101
当我在最后几页时,我希望它看起来与第一页相似:
1 ... 96 97 98 99 100 101
粗体数字是您当前正在查看的页面。
我希望这些点仅在可用页面超过 7 个时出现,否则它应该看起来像正常的分页:
1 2 3 4 5 6 7
我没有加粗,而是在其中添加了一些 CSS。
我的原始代码是这样的......
if (ListCount > ListPerPage)
{
if (Currentpage > PageCount)
{
Response.Redirect(Request.Path + "/?p=" + PageCount);
}
html += "<ul class=\"productListPaging\">";
for (int x = 1; x <= PageCount; x++)
{
if (x == Currentpage)
{
html += "<li class=\"active\">";
}
else
{
html += "<li>";
}
html += "<a href=\"javascript:void(0);\" onclick=\"changePage(" + x + ");\">";
html += x;
html += "</a> ";
html += "</li> ";
}
html += "</ul>";
}
此代码显示所有页面,而不是对其进行分组.. 我已经修改了上面的代码,但它只显示第一页和最后一页, 就像我有 10 页一样,它只显示第 1 页和第 10 页,而不显示 1 2 3 ... 10
任何帮助将不胜感激..
谢谢
约翰
求解算法
int before = 2;
int after = 2;
for (int x = 1; x <= Pagecount; x++)
{
if (x == CurrentPage)
{
if (x == Pagecount)
{
html += "";
}
else
{
html += "<li class=\"active\">";
#region Page Loop #
html += "<a href=\"" + Url;
html += querypage + x;
if (sortid != "")
{
html += querysort;
}
if (viewtype != "")
{
html += queryviews;
}
if (pricing != 0)
{
html += queryrange;
}
html += "\" >";
html += x;
html += "</a> ";
#endregion
html += "</li>";
}
}
else if (x < CurrentPage - before)
{
if (befli == 0)
{
html += "<li class=\"dotdotdot\">";
html += "<a href=\"#\">...</a>";
html += "</li>";
befli++;
}
}
else if (x > CurrentPage - before && x < CurrentPage + after)
{
if (x == Pagecount)
{
html += "";
}
else
{
html += "<li>";
#region Page Loop #
html += "<a href=\"" + Url;
html += querypage + x;
if (sortid != "")
{
html += querysort;
}
if (viewtype != "")
{
html += queryviews;
}
if (pricing != 0)
{
html += queryrange;
}
html += "\" >";
html += x;
html += "</a> ";
#endregion
html += "</li>";
}
}
else if (x > CurrentPage + after)
{
if (aftli == 0)
{
html += "<li class=\"dotdotdot\">";
html += "<a href=\"#\">...</a>";
html += "</li>";
aftli++;
}
}
else if (x == Pagecount)
{
html += "";
}
}
只需要使用大于或小于计算for循环
好的逻辑
'
int Before = #How Many Items Before Selected Number
int After = #How Many Items After Selected Number
int PageCount = #How Many Pages
int CurrentPage = #Current Page
//First Page
if (PageCount > 1)
{
// Here For Page Set Static 1
}
//Previous Button
if (CurrentPage != 1)
{
//Code Here (CurrentPage - 1)
}
for loop
for(int x = 1; x < PageCount; x++)
{
if (x == 1)
{
Page 1 //Static Page 1
if (x == CurrentPage)
{
//Bold Font / Highlight
}
else
{
//Normal
}
}
else if ( x == CurrentPage)
{
if(x == PageCount)
{
//None
}
else
{
//Bold Font / Highlight
}
}
else if (x < CurrentPage - Before)
{
// . . .
}
else if (x > CurrentPage - Before && x < CurrentPage + After)
{
if(x == PageCount)
{
//None
}
else
{
//Normal Font
}
}
else if (x > CurrentPage + After)
{
// . . .
}
else if (x == PageCount)
{
if (x == CurrentPage)
{
//Bold Highlight
}
else
{
//Normal
}
}
}
//Next Button
if (CurrentPage != PageCount)
{
//Code Here (CurrentPage + 1)
}
//First Page
if (PageCount > 1)
{
// Here For Page Set Static Last Page
}
'
希望我的逻辑对需要使用 for 循环进行分页的其他用户有所帮助。
约翰
【问题讨论】:
-
有大量的开源分页库。您可以考虑下载其中一个,看看他们是如何做到的。
标签: c# loops for-loop pagination paging