C#中的动态分页

时间:2012-03-26 10:29:00

标签: c# loops for-loop pagination paging

我是新来的

我在here

中遇到与Marcus相同的问题

我试图做的分页应该是这样的:

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

}

只需要使用大于或小于

来计算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循环的用户。

约翰

1 个答案:

答案 0 :(得分:0)

尝试这样的事情。您需要根据需要自定义它。

    /// <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();
  }