程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# .Net MVC Razor 視圖靜態分頁導航欄生成器

C# .Net MVC Razor 視圖靜態分頁導航欄生成器

編輯:C#入門知識

這裡貼出3個重要組成部分

1.分頁導航欄主算法代碼(包括 @Html 擴展方法)

2.HTML 頁面調用代碼(包括導航欄樣式)

3.Controller 控制器代碼

4.測試路徑:http://www.iqingcao.com/EBusiness/Paination/2?pc=10(這裡只是個本機測試url實例,不同機子測試 域名部分(www.iqingcao.com)需要替換一下)

   參數說明:pc(pageCount),總頁數

   其中的 2 是:pageIndex ,當前頁索引;使用的是默認路由也就是 {controller}/{action}/{id} 形式的路由定義

5.效果展示

 

注:實現效果簡單,如同學們有時間豐富一下配置參數,讓其可設置性更強些。

      豐富完了帖回來我也用哈

------------------------------------------------------------------------------------------------------------------------------

1.分頁導航欄主算法代碼

using System;
using System.Text;
using System.Web.Mvc;

namespace EB.Ctrl.Utility
{
  #region Mvc 分頁欄擴展方法
  /// <summary>
  ///  Mvc 分頁欄擴展方法
  /// </summary>
  public static class HtmlPaginationBar
  {
    /// <summary>
    /// 生成分頁欄(頁面調用 @Html.PaginationBar)
    /// </summary>
    /// <param name="helper"></param>
    /// <param name="bar">分頁欄生成器</param>
    /// <returns></returns>
    public static MvcHtmlString PaginationBar(this HtmlHelper helper, PaginationBarBilder bar)
    {
      return new MvcHtmlString(bar.GenPaginationHtml());
    }
  }

  #endregion

  #region 分頁條參數
  /// <summary>
  /// 分頁條參數
  /// </summary>
  public class PageBarPars
  {
    /// <summary>
    /// 總頁數
    /// </summary>
    public int PageCount { set; get; }
    /// <summary>
    /// 當前頁索引
    /// </summary>
    public int PageIndex { set; get; }
  }
  #endregion

  #region 分頁導航欄生成器
  public class PaginationBarBilder
  {
    /// <summary>
    /// 分頁導航欄生成器
    /// </summary>
    /// <param name="url">頁面地址模板,其中改變的頁面索引部分使用{0}替換</param>
    /// <param name="pageCount">總頁數</param>
    /// <param name="pageIndex">當前頁索引</param>
    public PaginationBarBilder(string url,int pageCount,int pageIndex)
    {
      m_url = url;
      m_pageCount = pageCount;
      m_pageIndex = pageIndex;
    }

    private int m_pageIndex = 0;
    private int m_pageCount = 0;
    private string m_url = string.Empty;
    private int m_offset = 3;
    private StringBuilder m_html = new StringBuilder();

    /// <summary>
    /// 生成分頁m_html代碼
    /// </summary>
    /// <returns></returns>
    public string GenPaginationHtml()
    {
      StringBuilder m_html = new StringBuilder();
      m_html.Append("<div id='pagination'>");
      m_html.Append(GenPrevious());
      m_html.Append(GenLeftAnchor());
      m_html.Append(GenMidArea());
      m_html.Append(GenRightAnchor());
      m_html.Append(GenNext());
      m_html.Append("</div>");
      return m_html.ToString();
    }
    /// <summary>
    /// 生成上一頁
    /// </summary>
    private string GenPrevious()
    {
      StringBuilder m_html = new StringBuilder();
      string href = string.Empty;
      if (m_pageIndex <= 0 || m_pageCount<=1)
      {
        m_html.Append("<span class='current prev'>前一頁</span>");
        return m_html.ToString();
      }
      href = string.Format(m_url, m_pageIndex-1);
      m_html.Append(
        string.Format("<a class='prev' href='{0}'>前一頁</a>", href));
      return m_html.ToString();
    }
    /// <summary>
    /// 生成後一頁
    /// </summary>
    private string GenNext()
    {
      StringBuilder m_html = new StringBuilder();
      string href = string.Empty;
      if (m_pageIndex >= m_pageCount-1)
      {
        m_html.Append("<span class='current next'>後一頁</span>");
        return m_html.ToString();
      }
      href = string.Format(m_url, ++m_pageIndex);
      m_html.Append(
        string.Format("<a class='prev' href='{0}'>後一頁</a>", href));
      return m_html.ToString();
    }
    /// <summary>
    /// 左停靠連接
    /// </summary>
    private string GenLeftAnchor()
    {
      StringBuilder m_html = new StringBuilder();
      string href = string.Empty;
      if (m_pageIndex - m_offset > 0)
      {
        href = string.Format(m_url, 0);
        m_html.Append(string.Format("<a href='{0}'>1</a>", href));
      }

      if (m_pageIndex - m_offset > 1)
        m_html.Append(string.Format("<span>...</span>"));
     
      return m_html.ToString();
    }
    /// <summary>
    /// 右停靠連接
    /// </summary>
    private string GenRightAnchor()
    {
      if (m_pageIndex + m_offset >= m_pageCount - 1)
        return string.Empty;

      StringBuilder m_html = new StringBuilder();
      string href = string.Empty;

      if (m_pageIndex + m_offset < m_pageCount-2)
        m_html.Append(string.Format("<span>...</span>"));

      if (m_pageIndex+m_offset<m_pageCount)
      {
        href = string.Format(m_url, m_pageCount - 1);
        m_html.Append(string.Format("<a href='{0}'>{1}</a>", href,m_pageCount));
      }

      return m_html.ToString();
    }
    /// <summary>
    /// 生成中間分頁按鈕部分
    /// </summary>
    private string GenMidArea()
    {
      StringBuilder m_html = new StringBuilder();
      string href = string.Empty;
      if (m_pageCount == 1)
      {
        m_html.Append("<span class='current'>1</span>");
        return m_html.ToString();
      }
      Action<int> addHtml = (index =>
      {
        if (index == m_pageIndex)
        {
          href = string.Format("<span class='current'>{0}</span>", index+1);
          m_html.Append(href);
          return;
        }
        href = string.Format(m_url,index);
        href = string.Format("<a href='{0}'>{1}</a>", href, index + 1);
        m_html.Append(href);
      });

      int start = 0;
      int end = 0;
      if (m_pageIndex < m_offset)
      {
        start = 0;
        if (m_offset + m_offset < m_pageCount)
          end = m_offset + m_offset;
        else
          end = m_pageCount - 1;
      }
      else
      {
        start = m_pageIndex - m_offset;
        if (m_pageIndex + m_offset >= m_pageCount)
          end = m_pageCount-1;
        else
          end = m_pageIndex + m_offset;
      }

      for (int i = 0; i < m_pageCount; i++)
      {
        if (i < start || i > end)
          continue;
        addHtml(i);
      }

      return m_html.ToString();
    }
  }
  #endregion
}
 

 

------------------------------------------------------------------------------------------------------------------------------

2.HTML 頁面調用代碼(包括導航欄樣式)

@{
  Layout = null;
}
@using EB.Ctrl.Utility;
<!DOCTYPE html>

<html>
<head>
    <title>Paination</title>
    <style>  
    #pagination{float:right}

    #pagination a {
        text-decoration: none;
     border: 1px solid #AAE;
     color: #666;
    }
    #pagination a:hover {
     BORDER-BOTTOM: #c00 1px solid; BORDER-LEFT: #c00 1px solid; BACKGROUND: #c00; COLOR: #fff; BORDER-TOP: #c00 1px solid; BORDER-RIGHT: #c00 1px solid; TEXT-DECORATION: none
    }
    #pagination a, #pagination span {
        display: inline-block;
        padding: 0.1em 0.4em;
        margin-right: 5px;
     margin-bottom: 5px;
    }

    #pagination .current {
        background: #bc0057;
        color: #fff;
     border: 1px solid #c00;
    }

    #pagination .current.prev, #pagination .current.next{
     color:#999;
     border-color:#999;
     background:#fff;
    }
    </style>
</head>
  <body>
    <!--使用這個擴展頁面必須引用 using EB.Ctrl.Utility 命名空間-->
    @Html.PaginationBar(new PaginationBarBilder("http://www.iqingcao.com/EBusiness/Paination/{0}?pc=" + this.ViewBag.PagePars.PageCount.ToString(), this.ViewBag.PagePars.PageCount, this.ViewBag.PagePars.PageIndex))
  </body>
</html>
 

 

------------------------------------------------------------------------------------------------------------------------------

3.Controller 控制器代碼

using System.Web.Mvc;
using EB.Ctrl.Utility;
using EB.Sys.Extension;

namespace EB.Ctrl.Controllers
{
  public class EBusinessController : Controller
  {

    public ActionResult Paination(string id,string pc)
    {
      PageBarPars pars = new PageBarPars();
      pars.PageCount = pc.GetInt();
      pars.PageIndex = id.GetInt();
      this.ViewBag.PagePars = pars;
      return View();
    }
  }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved