asp.net实现简单分页实例

具体实现方法如下:

复制代码 代码如下:

/// <summary>
/// 分页内容
/// </summary>
/// <param>页面大小</param>
/// <param>页面数量</param>
/// <param>当前页</param>
/// <param>url模式:demo.aspx?page=https://www.jb51.net/article/{0}</param>
/// <param>窗口模式</param>
/// <returns></returns>
public static string get_pagenation(int size,
                                    int count,
                                    int currendIndex,
                                    string pattern,
                                    string target)
{
    //1>打开窗口目标
    target = string.IsNullOrEmpty(target) ? "_top" : target;
    //2>总页数
    int pageCount = count / size;
    pageCount = pageCount * size == count ? pageCount : pageCount + 1;
    //3>分页内容
    StringBuilder strHtml = new StringBuilder();
    strHtml.Append("<span>");
 
    #region 首部处理
    if (currendIndex > 1)
    {
        strHtml.AppendFormat("<a href='https://www.jb51.net/article/1' target='https://www.jb51.net/article/{0}'>[首页]</a>", target);
        strHtml.AppendFormat("<a href='https://www.jb51.net/article/{0}' target='{1}'>[上一页]</a>", string.Format(pattern, currendIndex - 1), target);
    }
    else
    {
        strHtml.Append("<span>[首页]</span>&nbsp;&nbsp;<span>[上一页]</span>");
    }
    #endregion
 
    #region 中间部分
    int i = 1;
 
    int right = (currendIndex + 4) > pageCount ? pageCount : currendIndex + 4;
    if (currendIndex > 6)
    {
        i = currendIndex - 5;
    }
    else
    {
        right = pageCount >= 10 ? 10 : pageCount;
    }
    for (; i <= right; i++)
    {
        if (i == currendIndex)
        {
            strHtml.AppendFormat("<font>{0}</font>", i);
            strHtml.AppendLine();
            continue;
        }
        strHtml.AppendFormat("<a href='https://www.jb51.net/article/{0}' target='{1}'>[{2}]</a>", string.Format(pattern, i), target, i);
        strHtml.AppendLine();
    }
    #endregion
 
    #region 尾部处理
    if (currendIndex == pageCount)
    {
        strHtml.Append("<span>[下一页]</span><span>[末页]</span>");
        strHtml.AppendFormat("总共({0})页", pageCount);
    }
    else
    {
        strHtml.AppendFormat("<a href='https://www.jb51.net/article/{0}' target='{1}'>[下一页]</a>", string.Format(pattern, currendIndex + 1), target);
        strHtml.AppendFormat("<a href='https://www.jb51.net/article/{0}' target='{1}'>[末页]</a>", string.Format(pattern, pageCount), target);
        strHtml.AppendFormat("&nbsp;&nbsp;<label>总共({0})页</label>", pageCount);
    }
    #endregion
 
    strHtml.Append("</span>");
 
    return strHtml.ToString();
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjzwgd.html