C#中的动态分页 [英] Dynamic Pagination in C#

查看:179
本文介绍了C#中的动态分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的



我和Marcus在

我正在尝试做的分页应该是这样的:



1 2 3 4 5 6 ... 101



当我点击数字5时,喜欢它显示这样的数字:

1 ... 3 4 5强6 7 ... 101 $ / b $
$ b

当我在最后几页我想看起来类似于第一个:

1 ... 96 97 $ 99 $ 100 $

粗体数字是您当前正在查看的页面。



我希望只在有多于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>& nbsp;;
html + =< / li>& nbsp;;
}
html + =< / ul>;





这段代码显示所有的页面,而不是分组..
我已经修改了上面的代码,但它只显示第一个和最后一个页面,
就像我有10个页面,它只显示页面1和10,而不是1 2 3 ... 10

$ b $
$ b

任何帮助将不胜感激。 >

解决算法

  int before = 2; 
int后= 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 (viewtype!=)

html + = queryviews)
{
html + = querysort;
}
;
}
if(pricing!= 0)
{
html + = queryrange;
}

html + =\ >;
html + = x;
html + =< / a>& nbsp;;
#endregion
html + =< / li>;


else if(x {
if(befli == 0)
{
html + =< li class = \dotdotdot \>;
html + =< a href = \#\> ...< / a>;
html + =< / li>;
befli ++;

$ b $ 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 (viewtype!=)

html + = queryviews)
{
html + = querysort;
}
;
}
if(pricing!= 0)
{
html + = queryrange;
}

html + =\ >;
html + = x;
html + =< / a>& nbsp;;
#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 + =;

code










$ b $ p


$ b

只需要计算for循环使用大于或小于

确定逻辑



'

  int之前=#如何在选定的数字之前的许多项目
int之后=#如何选择数量之后的许多项目

int PageCount =#许多页面
int CurrentPage =#当前页面

//第一页
if(PageCount> 1)
{
//这里为页面设置静态1
}
//上一个按钮
if(CurrentPage!= 1)
{
//此处的代码(CurrentPage - 1)


for循环
for(int x = 1; x {
if(x == 1 )
{
Page 1 //静态页面1
if(x == CurrentPage)
{
//粗体字体/突出显示
}

{
//正常
}
}
else if(x == CurrentPage)
{
if(x = = PageCount)
{
// None
}
else
{
//粗体字体/突出显示
}
}
else else if(x < CurrentPage - Before)
{
//。 。 。

else if(x> CurrentPage - Before&& x< CurrentPage + After)
{
if(x == PageCount)
{
// None
}
else
{
//普通字体
}
}
else if(x> CurrentPage + After)
{
//。 。 。

else if(x == PageCount)
{
if(x == CurrentPage)
{
// Bold Highlight
}
else
{
//正常
}
}
}

//下一个按钮
if (CurrentPage!= PageCount)
{
// Code Here(CurrentPage + 1)
}

//第一页
if(PageCount> 1)
{
//这里为页面设置静态最后一页
}

'



希望我的逻辑能够帮助其他需要使用for循环分页的用户。

Johan

解决方案

试试这样的事情。您需要根据您的需要定制它。

  ///< summary> 
///构建分页HTML。
///< / summary>
///< param name =currentpage>当前所选页面< / param>
///< param name =totalPages>分页总页数< / param>
///< param name =dotsApearanceCount>点的数目。你想要多少个点(。)< / param>
///< param name =groupCount>组计数。根据所选页面构建的组< / param>
///<返回>< /返回>
public string BuildPagingHTML(int currentpage,int totalPages,int dotsApearanceCount,int groupCount)
{
StringBuilder sbPagingHtml = new StringBuilder();
sbPagingHtml.Append(< ul class = \productListPaging \>);
//显示第一页
sbPagingHtml.Append(< li>);
sbPagingHtml.Append(< a href = \javascript:void(0); \onclick = \changePage(+ 1 +); \>);
sbPagingHtml.Append(1);
sbPagingHtml.Append(< / a>& nbsp;);
sbPagingHtml.Append(< / li>& nbsp;);
if(totalPages> 1&& currentpage - 2> = 1)
{
sbPagingHtml.Append(GenerateDots(dotsApearanceCount)); (var linkCount = currentpage - 2; linkCount< = currentpage + 2; linkCount ++)



$ b if(linkCount> = 2&& amp; ; linkCount< = totalPages - 2)
{
if(currentpage == linkCount)
{
sbPagingHtml.Append(< li class ='active'> );
}
else
{
sbPagingHtml.Append(< li>);


sbPagingHtml.Append(< a href = \javascript:void(0); \onclick = \changePage(+ linkCount +); \ > 中);
sbPagingHtml.Append(linkCount);
sbPagingHtml.Append(< / a>& nbsp;);
sbPagingHtml.Append(< / li>& nbsp;);
}
}

sbPagingHtml.Append(GenerateDots(dotsApearanceCount));

//显示最后一页
sbPagingHtml.Append(< li>);
sbPagingHtml.Append(< a href = \javascript:void(0); \onclick = \changePage(+ totalPages +); \>);
sbPagingHtml.Append(totalPages);
sbPagingHtml.Append(< / a>& nbsp;);
sbPagingHtml.Append(< / li>& nbsp;);
}

sbPagingHtml.Append(< / ul>);
返回sbPagingHtml.ToString();
}

///< summary>
///生成点。
///< / summary>
///< param name =numberofDots>点数。< / param>
///<返回>< /返回>
public string GenerateDots(int numberofDots)
{
StringBuilder sbPagingHtml = new StringBuilder(); (var dotCount = 1; dotCount< = numberofDots; dotCount ++)

{
sbPagingHtml.Append(< li>)
sbPagingHtml.Append(< a>);
sbPagingHtml.Append(。);
sbPagingHtml.Append(< / a>& nbsp;);
sbPagingHtml.Append(< / li>& nbsp;);
}

return sbPagingHtml.ToString();
}


I'm new here

I have the same problem as Marcus in here

the paging I'm trying to do should look something like this:

1 2 3 4 5 6 ... 101

When i click on number 5 i would like it to display the numbers like this:

1 ... 3 4 5 6 7 ... 101

when I'm at the last couple of pages i want it to look similar to the first one:

1 ... 96 97 98 99 100 101

The bold number is the page that you're currently viewing.

I want the dots to appear only when there is more than 7 pages available, if not it should look like a normal paging would look like:

1 2 3 4 5 6 7

instead of making it bold, i have added added a few CSS in it..

my original code is something like this...

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>&nbsp;";
                html += "</li> &nbsp;";
            }
            html += "</ul>";
        }

this code display all the pages, not grouping it.. I have modified the code above, but it only displays the first and last Pages, Like if I have 10 Pages it only displays page 1 and 10 and not 1 2 3 ... 10

Any Help Would be appreciated..

Thanks

Johan

Solved the Algorithm

        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>&nbsp;";
        #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>&nbsp;";
        #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 += "";
}

}

Just Needed to calculate the for loop using the greater or less than

Ok the Logic

'

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
}

'

Hope My Logic Helps For Other Users Who Needs Pagination using for loops.

Johan

解决方案

Try something like this. You need to customize it as per your need.

    /// <summary>
  /// Builds the paging HTML.
  /// </summary>
  /// <param name="currentpage">The current selected page.</param>
  /// <param name="totalPages">The total pages for paging.</param>
  /// <param name="dotsApearanceCount">The dots apearance count. How many dots (.) you want</param>
  /// <param name="groupCount">The group count. Group that is build based on selected page</param>
  /// <returns></returns>
  public string BuildPagingHTML(int currentpage, int totalPages, int dotsApearanceCount, int groupCount)
  {
    StringBuilder sbPagingHtml = new StringBuilder();
    sbPagingHtml.Append("<ul class=\"productListPaging\">");
    // Display the first page
    sbPagingHtml.Append("<li>");
    sbPagingHtml.Append("<a href=\"javascript:void(0);\" onclick=\"changePage(" + 1 + ");\">");
    sbPagingHtml.Append(1);
    sbPagingHtml.Append("</a>&nbsp;");
    sbPagingHtml.Append("</li> &nbsp;");
    if (totalPages > 1 && currentpage - 2 >= 1)
    {
      sbPagingHtml.Append(GenerateDots(dotsApearanceCount));


      for (var linkCount = currentpage - 2; linkCount <= currentpage + 2; linkCount++)
      {
        if (linkCount >= 2 && linkCount <= totalPages - 2)
        {
          if (currentpage == linkCount)
          {
            sbPagingHtml.Append("<li class='active'>");
          }
          else
          {
            sbPagingHtml.Append("<li>");
          }

          sbPagingHtml.Append("<a href=\"javascript:void(0);\" onclick=\"changePage(" + linkCount + ");\">");
          sbPagingHtml.Append(linkCount);
          sbPagingHtml.Append("</a>&nbsp;");
          sbPagingHtml.Append("</li> &nbsp;");
        }
      }

      sbPagingHtml.Append(GenerateDots(dotsApearanceCount));

      // Display the last page
      sbPagingHtml.Append("<li>");
      sbPagingHtml.Append("<a href=\"javascript:void(0);\" onclick=\"changePage(" + totalPages + ");\">");
      sbPagingHtml.Append(totalPages);
      sbPagingHtml.Append("</a>&nbsp;");
      sbPagingHtml.Append("</li> &nbsp;");
    }

    sbPagingHtml.Append("</ul>");
    return sbPagingHtml.ToString();
  }

  /// <summary>
  /// Generates the dots.
  /// </summary>
  /// <param name="numberofDots">The numberof dots.</param>
  /// <returns></returns>
  public string GenerateDots(int numberofDots)
  {
    StringBuilder sbPagingHtml = new StringBuilder();
    for (var dotCount = 1; dotCount <= numberofDots; dotCount++)
    {
      sbPagingHtml.Append("<li>");
      sbPagingHtml.Append("<a>");
      sbPagingHtml.Append(".");
      sbPagingHtml.Append("</a>&nbsp;");
      sbPagingHtml.Append("</li> &nbsp;");
    }

    return sbPagingHtml.ToString();
  }

这篇关于C#中的动态分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆