MVC的一個分頁,網上摘錄,自己少量修改

來源:互聯網
上載者:User
分頁類

using System.Text;
using System.Web.Routing;

namespace System.Web.Mvc
{
    public static class PagerHelper
    {
        /// <summary>
        /// 擷取分頁
        /// </summary>
        /// <param name="urlFormat">url格式,用{0}代替頁碼</param>
        /// <param name="pageIndex">當前頁</param>
        /// <param name="pageSize">總頁數</param>
        /// <param name="recordCount">總記錄數</param>
        /// <param name="mode">分頁模式</param>
        /// <returns>html</returns>
        public static string Pager(this HtmlHelper helper, string urlFormat, int pageIndex, int pageSize, int recordCount, PageMode mode)
        {
            urlFormat = urlFormat.Replace("%7B0%7D", "{0}");
            int pageCount = (recordCount % pageSize == 0 ? recordCount / pageSize : recordCount / pageSize + 1);
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("總共{0}條記錄,共{1}頁,當前第{2}頁&nbsp;&nbsp;", recordCount, pageCount, pageIndex);
            if (pageIndex == 1)
                sb.Append("首頁&nbsp;");
            else
            {
                string url1 = string.Format(urlFormat.ToString(), 1);
                sb.AppendFormat("<a href={0}>首頁</a>&nbsp;", url1);
            }
            if (pageIndex > 1)
            {
                string url1 = string.Format(urlFormat.ToString(), pageIndex - 1);
                sb.AppendFormat("<a href={0}>上一頁</a>&nbsp;", url1);
            }
            else
                sb.Append("上一頁&nbsp;");
            if (mode == PageMode.Numeric)
                sb.Append(GetNumericPage(urlFormat.ToString(), pageIndex, pageSize, recordCount, pageCount));
            if (pageIndex < pageCount)
            {
                string url1 = string.Format(urlFormat.ToString(), pageIndex + 1);
                sb.AppendFormat("<a href={0}>下一頁</a>&nbsp;", url1);
            }
            else
                sb.Append("下一頁&nbsp;");

            if (pageIndex == pageCount)
                sb.Append("末頁");
            else
            {
                string url1 = string.Format(urlFormat.ToString(), pageCount);
                sb.AppendFormat("<a href={0}>末頁</a>", url1);
            }
            return sb.ToString();
        }

        /// <summary>
        /// 擷取數字分頁
        /// </summary>
        private static string GetNumericPage(string url, int pageIndex, int pageSize, int recordCount, int pageCount)
        {
            int k = pageIndex / 10;
            int m = pageIndex % 10;
            StringBuilder sb = new StringBuilder();
            if (pageIndex / 10 == pageCount / 10)
            {
                if (m == 0)
                {
                    k--;
                    m = 10;
                }
                else
                    m = pageCount % 10;
            }
            else
                m = 10;
            for (int i = k * 10 + 1; i <= k * 10 + m; i++)
            {
                if (i == pageIndex)
                    sb.AppendFormat("<span style=\"color: red\">{0}</span>&nbsp;", i);
                else
                {
                    string url1 = string.Format(url.ToString(), i);
                    sb.AppendFormat("<a href={0}>{1}</a>&nbsp;", url1, i);
                }
            }

            return sb.ToString();
        }
    }

    /// <summary>
    /// 分頁模式
    /// </summary>
    public enum PageMode
    {
        /// <summary>
        /// 普通分頁模式
        /// </summary>
        Normal,
        /// <summary>
        /// 普通分頁加數字分頁
        /// </summary>
        Numeric
    }
}

 

<%= Html.Pager(Url.Action("Main",new {page="{0}"}),10,10,1000, PageMode.Numeric) %>

 

以{0}代替頁索引的參數即可,使用非常方便.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.