MVC 實現分頁效果

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   ar   color   os   使用   

 public static HtmlString ShowPageNavigate(this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount)        {            var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath;            pageSize = pageSize == 0 ? 3 : pageSize;            var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1);//總頁數            var output = new StringBuilder();            if (totalPages > 1)            {                {//處理首頁串連                    output.AppendFormat("<a class=‘pageLink‘ href=‘javascript:void(0)‘ onclick=‘pageShow(\"{0}\",1,{1});‘>首頁</a>", redirectTo, pageSize);                }                if (currentPage > 1)                {                    //處理上一頁的串連                    output.AppendFormat("<a class=‘pageLink‘ href=‘javascript:void(0)‘ onclick=‘pageShow(\"{0}\",{1},{2});‘>上一頁</a>", redirectTo, currentPage - 1, pageSize);                }                else                {                    //output.Append("<span class=‘pageLink‘>上一頁</span>");                }                output.Append(" ");                int currint = 5;                for (int i = 0; i <= 10; i++)                {                    if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)                    {                        if (currint == i)                        {                            //當前頁處理                            output.AppendFormat("<a class=‘on‘ href=‘javascript:void(0)‘ onclick=‘pageShow(\"{0}\",{1},{2});‘ id=‘oncurentPage‘>{3}</a>", redirectTo, currentPage, pageSize, currentPage);                        }                        else                        {                            //一般頁處理                            output.AppendFormat("<a class=‘pageLink‘ href=‘javascript:void(0)‘ onclick=‘pageShow(\"{0}\",{1},{2});‘>{3}</a>", redirectTo, currentPage + i - currint, pageSize, currentPage + i - currint);                        }                    }                    output.Append(" ");                }                if (currentPage < totalPages)                {                    //處理下一頁的串連                    output.AppendFormat("<a class=‘pageLink‘ href=‘javascript:void(0)‘ onclick=‘pageShow(\"{0}\",{1},{2});‘>下一頁</a>", redirectTo, currentPage + 1, pageSize);                }                else                {                }                output.Append(" ");                if (currentPage != totalPages)                {                    output.AppendFormat("<a class=‘pageLink‘ href=‘javascript:void(0)‘ onclick=‘pageShow(\"{0}\",{1},{2});‘>末頁</a>", redirectTo, totalPages, pageSize);                }                output.Append(" ");                output.AppendFormat("第{0}頁 / 共{1}頁", currentPage, totalPages);            }            return new HtmlString(output.ToString());        }
方法
@model System.Data.DataTable@{    Layout = null;}<div class="box-frame">    <h3>        待辦工作</h3>    <table class="box-frame-table wordlist-table">        <thead>            <tr>                <th>                    主題                </th>                <th width="100">                    寄件者                </th>                <th width="130">                    時間                </th>            </tr>        </thead>        <tbody>            @foreach (System.Data.DataRow item in Model.Rows)            {                 <tr tag="@item["Dia_Id"]">                    <td>@item["Pro_Title"]                    </td>                    <td>@item["C_aname"]                    </td>                    <td>@string.Format("{0:g}", item["Dia_StartTime"])                    </td>                </tr>            }        </tbody>    </table></div><p class="page_nav">    @Html.ShowPageNavigate(Convert.ToInt32(ViewData["pageIndex"]), Convert.ToInt32(ViewData["pageSize"]), Convert.ToInt32(ViewData["totalCount"]))</p><script type="text/javascript">    $(‘.wordlist-table tr‘).bind(‘click‘, function (event) {        window.location.href = "#test/todo";        analyseUrl("#test/todo");    });</script></body> </html> 
前台顯示
        int pageIndex = 1;//當前頁        int pageSize = 3;//每頁顯示條數  public ActionResult ToDoInfoList()        {            c_addressbook m_addressbook = Common.ReadCookie();            if (m_addressbook != null)            {                int.TryParse(Request.QueryString["pageIndex"], out pageIndex);                if (pageIndex == 0)                    pageIndex = 1;                string sqlCount = "select Dia_Id,pc_process.Pro_Title,pc_process.Pro_Sender,c_addressbook.C_aname,pc_dialog.Dia_StartTime from pc_dialog LEFT JOIN pc_process ON pc_dialog.Pro_Id =pc_process.Pro_id LEFT JOIN c_addressbook ON pc_process.Pro_Sender=c_addressbook.C_aid where [email protected] and pc_dialog.Dia_State=-1";                string sql = "select Dia_Id,pc_process.Pro_Title,pc_process.Pro_Sender,c_addressbook.C_aname,pc_dialog.Dia_StartTime from pc_dialog LEFT JOIN pc_process ON pc_dialog.Pro_Id =pc_process.Pro_id LEFT JOIN c_addressbook ON pc_process.Pro_Sender=c_addressbook.C_aid where [email protected] and pc_dialog.Dia_State=-1 order by Dia_StartTime desc LIMIT " + (pageIndex - 1) * pageSize + "," + pageSize + "";                MySqlParameter[] pars = new MySqlParameter[]{                new MySqlParameter("@manager",m_addressbook.C_aid)                };                int totalCount = PM.DBUtility.DbHelperMySQL.Query(sqlCount, pars).Tables[0].Rows.Count;                int pageCount = Convert.ToInt32(Math.Ceiling((double)totalCount / pageSize));//總頁數                                if (pageIndex < 1)                    pageIndex = 1;                else if (pageIndex > pageCount)                    pageIndex = pageCount;                var newPage = PM.DBUtility.DbHelperMySQL.Query(sql, pars).Tables[0];                if (newPage.Rows.Count != 0)                {                    ViewData.Model = newPage;                    ViewData["pageCount"] = pageCount;                    ViewData["pageIndex"] = pageIndex;                    ViewData["pageSize"] = pageSize;                    ViewData["totalCount"] = totalCount;                }                ViewData.Model = newPage;            }            return View();        }
後台資料 使用方法

 

MVC 實現分頁效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.