• MVC 分页


    写了一个MVC分页类

     public class Paging
        {

            private Regex regex = new Regex(@"^[0-9]+$");
            private int showIcons = 6;//显示的分页按钮数目
            private int currentPageIndex;//当前页数
            private int totalCount;
            private int pageSize;
            private string routeDataPage;
            public NameValueCollection QueryString{get;set;}
            public string Url{get;set;}
            public string ParseUrl { get; set; }
            public string CssClassName { get; set; }
            
            public string RouteDataPage
            {
                get
                {
                    return routeDataPage;
                }
                set
                {
                    routeDataPage = value;
                    if (value == string.Empty||value==null)
                    {
                        CurrentPageIndex = 1;
                    }
                    else
                    {
                        CurrentPageIndex = StrToInt(value.TrimStart('p').TrimStart('P'), 1);
                    }

                }
            }
            public int ShowIcons
            {
                get
                {
                    return showIcons;
                }
                set
                {
                    this.showIcons = value;
                }

            }
            public int TotalCount//总数据条数
            {
                get
                {
                    return totalCount;
                }
                set
                {
                    totalCount = value;
                    GetPageCount();
                }

            }
            public int PageSize//单页呈现的数目
            {
                get
                {
                    return pageSize;
                }
                set
                {
                    pageSize = value;
                    GetPageCount();
                }

            }
            public int PageCount//总页数
            {
                get;
                set;

            }
            
            public int CurrentPageIndex
            {
                get
                {
                    return currentPageIndex;
                }
                set
                {
                    if (!regex.IsMatch(value.ToString()))
                    {
                        currentPageIndex = 1;
                    }
                    else
                    {
                        if (value == 0||(value > PageCount && PageCount == 0))
                        {
                            currentPageIndex = 1;
                        }
                        else if(value>PageCount&&PageCount!=0)
                        {
                            currentPageIndex = PageCount;
                        }
                        else
                        {
                            currentPageIndex = value;
                        }
                    }
                }
            }
            public Paging(int total,string routeDataPage,int pageSize,string url,NameValueCollection queryString)
            {
                this.TotalCount = total;
                this.PageSize = pageSize;
                GetPageCount();
                this.RouteDataPage = routeDataPage;
                this.Url = url;
                this.QueryString = queryString;
                HttpContext.Current.RewritePath(url,true);
                Url = HttpContext.Current.Request.FilePath;
                //GetPageCount();

            }

            public void GetPageCount()//获取总页数
            {
                if (pageSize == 0) return;
                
                this.PageCount = (TotalCount % PageSize > 0) ? (TotalCount / PageSize + 1) : (TotalCount / PageSize);
            
            }

            public void GetParseUrl()
            {
                if (QueryString.Count == 0 )
                {
                    ParseUrl =Url+"p{0}";
                }
                else
                {
                    StringBuilder Paras = new StringBuilder("");
                    foreach(string key in QueryString.AllKeys)
                    {
                        
                            Paras.Append("&" + key + "=" +HttpContext.Current.Server.UrlEncode(HttpContext.Current.Server.UrlDecode(QueryString[key])));
                        
                    }
                    ParseUrl =Url+"p{0}?" + Paras.ToString().TrimStart('&');
                
                }
            
            }

            //记录总数量  单页数量大小  当前页 最大初始化页数(1 2 3 4 5 6...20)示例为6 要求  链接页面
            public string GetPagerStr()
            {
                GetParseUrl();
                StringBuilder sb = new StringBuilder();
                if (TotalCount == 0)
                {
                    return string.Empty;
                }
                string BeginStr=GetBeginStr();
                string EndStr=GetEndStr();

                //页数如果小于等于最大初始化呈现页数
                if (PageCount <= (ShowIcons + 1))
                {
                    sb.Append(BeginStr);
                    sb.Append(GetMiddleStr(1,PageCount,ParseUrl));
                    sb.Append(EndStr);
                }
                else
                {
                    if (CurrentPageIndex < ShowIcons) //如果当前页小于等于初始化数目
                    {
                        sb.Append(BeginStr);
                        sb.Append(GetMiddleStr( 1,showIcons,ParseUrl));
                        sb.Append("<span>...</span><a href=\"" + String.Format(ParseUrl,(PageCount).ToString()) + "\">" + PageCount.ToString() + "</a>");
                        sb.Append(EndStr);
                    }
                    else//如果当前页大于初始化页面
                    {
                        if (CurrentPageIndex > (PageCount - ShowIcons + 1))//满足后几页
                        {
                            sb.Append(BeginStr);
                            sb.Append("<a href=\"" + String.Format(ParseUrl,1) + "\">1</a> <span>...</span> ");
                            sb.Append(GetMiddleStr(PageCount - ShowIcons + 1, PageCount,ParseUrl));
                            sb.Append(EndStr);
                        }
                        else//满足处在中间
                        {
                            sb.Append(BeginStr);
                            sb.Append("<a href=\"" + String.Format(ParseUrl,1) + "\">1</a> <span>...</span> ");
                            sb.Append(GetMiddleStr(CurrentPageIndex - (ShowIcons - 2) / 2, CurrentPageIndex + (ShowIcons - 2) / 2,ParseUrl));
                            sb.Append(" <span>...</span> <a href=\"" +String.Format(ParseUrl,(PageCount).ToString()) + "\">" + PageCount.ToString() + "</a>");
                            sb.Append(EndStr);
                        }
                    }
                }
                return sb.ToString();
            }

            //中间循环代码
            public string GetMiddleStr(int StartIndex,int EndIndex,string ParseUrl)
            {
                StringBuilder MiddleSb = new StringBuilder();
                for (int i = StartIndex; i <= EndIndex; i++)
                {
                    if (i != CurrentPageIndex)
                    {
                        MiddleSb.Append("<a href=\"" +String.Format(ParseUrl, i.ToString()) + "\">" + i.ToString() + "</a>");
                    }
                    else
                    {
                        MiddleSb.Append("<span class=\"current\">" + i.ToString() + "</span>");
                    }
                }
                return MiddleSb.ToString();
            }

            //获取开始代码
            public string GetBeginStr()
            {
                string BeginStr;
                //如果当前页数是首页  上一页无效
                if (CurrentPageIndex == 1)
                {
                    BeginStr = "<div class=\"" + CssClassName + "\"><span class=\"info\">共" + TotalCount.ToString() + "条记录 " + PageCount + "页 当前第 1 页</span><span class=\"num\">";
                }
                else
                {
                    BeginStr = "<div class=\"" + CssClassName + "\"><span class=\"info\">共" + TotalCount.ToString() + "条记录 " + PageCount + "页 当前第 " + CurrentPageIndex.ToString() + " 页</span><span class=\"num\"><a href=\"" + string.Format(ParseUrl,(CurrentPageIndex - 1).ToString()) + "\">上一页</a>";
                }
                return BeginStr;
            
            }

            //获取结尾代码
            public string GetEndStr()
            {
                string EndStr;
                //判断尾页
                if (CurrentPageIndex == PageCount)
                {
                    EndStr = "</span></div>";
                }
                else
                {
                    EndStr = "<a href=\"" + String.Format(ParseUrl, (CurrentPageIndex + 1).ToString()) + "\">下一页</a></span></div>";
                }
                return EndStr;

            }

            /// <summary>
            /// string型转换为int型
            /// </summary>
            public static int StrToInt(string strValue, int defValue)
            {
                if (string.IsNullOrEmpty(strValue)) return defValue;

                if (!int.TryParse(strValue.ToString(), out defValue)) return defValue;
                return defValue;
            }

          
        }

    调用

     [ActionName("List")]
            public ActionResult BookList(string id)
            {
                MyDataContext mdc = new MyDataContext();
                int count = SqlHelper.GetDataCount("select count(*) from book");
                Paging paging = new Paging(count, id, 1, "/Book/List/", Request.QueryString);
                int currentPageIndex = paging.CurrentPageIndex-1;
                int pageSize = paging.PageSize;
                var books = mdc.ExecuteQuery<Book>("select a.bookname, b.authorname from  book as a left join Author as b on a.authorid=b.authorid");
                books=books.Skip(currentPageIndex * pageSize).Take(pageSize);
                BookPager bookPager = new BookPager();
                bookPager.Books = books.ToList();
               bookPager.Paging = paging;
                return View(bookPager);
            }

        <div>
        <% foreach(Book bk in Model.Books){ %>
        作者:<%= bk.AuthorName %>书本:<%: bk.BookName %>
        <%} %>
        <%= Model.Paging.GetPagerStr()%>
        
        </div>

  • 相关阅读:
    整个过程
    iframe 重新加载闪过白块问题
    C# 获得两日期之间所有月份(包括跨年)
    新手是个框,啥都往里装!---谨以此文致歉博友和自己的无知
    C# 和Java的foreach的不同用法
    终于鼓起勇气,辞掉了第一份工作
    Java Junit4测试功能
    自学Android的第一个小程序(小布局、button点击事件、toast弹出)
    JS--Div中数据滚动到最后一条重新从头开始滚动
    RelativeLayout与LinearLayout的区别
  • 原文地址:https://www.cnblogs.com/yuhanzhong/p/2954662.html
Copyright © 2020-2023  润新知