码迷,mamicode.com
首页 > Web开发 > 详细

Mvc分页扩展类 分页代码 带跳转

时间:2015-03-20 14:20:00      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

  public static class HtmlPager
    {
        public static MvcHtmlString MvcPager(this HtmlHelper html, string currentPageStr, int pageSize, int totalCount)
        {
            var queryString = html.ViewContext.HttpContext.Request.QueryString;
            int currentPage = 1; //当前页  
            var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数  
            var dict = new System.Web.Routing.RouteValueDictionary(html.ViewContext.RouteData.Values);
            var output = new StringBuilder();
            StringBuilder urlformat = new StringBuilder();//url地址
            foreach (string item in dict.Keys)
            {
                urlformat.Append("/" + dict[item].ToString());
            }
            urlformat.Append("?");
            if (!string.IsNullOrEmpty(queryString[currentPageStr]))
            {
                //与相应的QueryString绑定 
                foreach (string key in queryString.Keys)
                    if (queryString[key] != null && !string.IsNullOrEmpty(key))
                        dict[key] = queryString[key];
                int.TryParse(queryString[currentPageStr], out currentPage);
            }
            else
            {
                //获取 ~/Page/{page number} 的页号参数
                if (dict.ContainsKey(currentPageStr))
                    int.TryParse(dict[currentPageStr].ToString(), out currentPage);
            }
            foreach (string item in queryString.Keys)
            {
                if (item != currentPageStr)
                {
                    urlformat.Append(item + "=" + queryString[item].ToString() + "&");
                }
            }
            urlformat.Append(currentPageStr + "=");
            string strUrlformat = urlformat.ToString();
            //保留查询字符到下一页
            foreach (string key in queryString.Keys)
                dict[key] = queryString[key];

            //如果有需要,保留表单值到下一页 (我暂时不需要, 所以注释掉)
            //var formValue = html.ViewContext.HttpContext.Request.Form;
            //foreach (string key in formValue.Keys)
            //    if (formValue[key] != null && !string.IsNullOrEmpty(key))
            //        dict[key] = formValue[key]; 

            if (currentPage <= 0) currentPage = 1;
            if (totalPages > 0)
            {
                output.AppendFormat("共{0}页 ", totalPages);//这个统计加不加都行 
                //if (currentPage != 1)//第一页时不显示
                //{
                //处理首页连接  永远显示
                //dict[currentPageStr] = 1;
                //output.AppendFormat("{0} ", html.RouteLink("首页", dict));
                output.Append("<a href=‘" + urlformat + "1‘>首页</a>");
                //}
                if (currentPage > 1)
                {
                    //处理上一页的连接  
                    //dict[currentPageStr] = currentPage - 1;
                    //output.Append(html.RouteLink("上一页", dict));
                    output.Append("<a href=‘" + strUrlformat + (currentPage - 1) + "‘>上一页</a>");
                }
                else
                {
                    output.Append("<a href=‘javascript:void(0);‘>上一页</a>");
                    //output.Append("上一页");
                }
                output.Append(" ");
                int currint = 2;
                for (int i = 0; i <= 4; i++)
                {
                    //一共最多显示5个页码,前面2个,后面2个  
                    if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
                        if (currint == i)
                        {
                            //当前页处理  
                            output.Append(string.Format("<a class=‘Cur01‘ href=‘javascript:void(0);‘>{0}</a>", currentPage));
                            //output.Append(string.Format("[{0}]", currentPage));
                        }
                        else
                        {
                            //一般页处理 
                            //dict[currentPageStr] = currentPage + i - currint;
                            //output.Append(html.RouteLink((currentPage + i - currint).ToString(), dict));
                            output.Append("<a href=‘" + strUrlformat + (currentPage + i - currint) + "‘>" + (currentPage + i - currint) + "</a>");
                        }
                    output.Append(" ");
                }
                if (currentPage < totalPages)
                {
                    //处理下一页的链接 
                    //dict[currentPageStr] = currentPage + 1;
                    //output.Append(html.RouteLink("下一页", dict));
                    output.Append("<a href=‘" + strUrlformat + (currentPage + 1) + "‘>下一页</a>");
                }
                else
                {
                    output.Append("<a href=‘javascript:void(0);‘>下一页</a>");
                    //output.Append("下一页");
                }
                output.Append(" ");
                //if (currentPage != totalPages)//最后一页时不显示
                //{
                //dict[currentPageStr] = totalPages;
                //output.Append(html.RouteLink("尾页", dict));
                output.Append("<a href=‘" + strUrlformat + (totalPages) + "‘>尾页</a>");
                //}
                output.Append(" ");
                //output.AppendFormat("{0} / {1}", currentPage, totalPages);//这个统计加不加都行 

                output.Append(html.TextBox("JumpPage", null, new { Class = "TJump", onkeydown = "return EnterPress(event)" }));
                output.Append(" ");
                output.Append("<input type=‘button‘ value=‘Go‘ id=‘btnJump‘ data-maxpages=‘" + totalPages + "‘ data-urlformat=‘" + HttpContext.Current.Server.HtmlEncode(strUrlformat) + "‘ onclick=‘return gotopage()‘ />");
            }
            string rtnStr = output.ToString();
            string flagStr = @"[\?&]page=1(?!\d)";//?!负声明,涵义:括号中的模式必须不出现在声明右侧
            rtnStr = Regex.Replace(rtnStr, flagStr, "");
            return new MvcHtmlString(rtnStr);
        }
    }

 

function gotopage() { var pagetarget = $("#JumpPage").val(); var r = new RegExp("^\\s*(\\d+)\\s*$"); if (!r.test(pagetarget)) { alert("请输入有效数字!"); return; } else if (RegExp.$1 < 1 || RegExp.$1 > parseInt($("#btnJump").attr("data-maxpages"))) { alert("超出范围!"); return; } else { location.href = decodeURI($("#btnJump").attr("data-urlformat") + pagetarget) } }

@Html.MvcPager("page", pageSize, count)

Mvc分页扩展类 分页代码 带跳转

标签:

原文地址:http://www.cnblogs.com/huhaihua/p/4353420.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!