zoukankan      html  css  js  c++  java
  • asp.net mvc3分页代码

     最近在学asp.net mvc3,与asp.net webForm比起来。确实能提高不少效率,特别是数据校验和数据源方面,基本上不用自己手写。但是总担心其entity framework框架,会不会影响程序运行效率,下面是我自己写的分页代码,欢迎大家指教

      public ActionResult Index(int page)
            {
                int pageSize = 1;
                var query=ob.news.OrderByDescending(news=>news.id).Skip(pageSize*(page-1)).Take(pageSize);
                pageing pageInfo = new pageing();
                pageInfo.pageSize = pageSize;
                pageInfo.curentPage = page;
                pageInfo.totalCount = ob.news.Count();
                pageInfo.coustomUrl = "/news?";
                ViewBag.Page = pageInfo;
                return View(query);
              
            }

    public class pageing
        {  
            /// <summary>
            /// 每页数量
            /// </summary>
            public int pageSize { get; set; }
           /// <summary>
            /// 当前页
           /// </summary>
            public int curentPage{ get; set; }
            /// <summary>
            /// 总数量
            /// </summary>
            public int totalCount { get; set; }
            /// <summary>
            /// 定义页页连接
            /// </summary>
            public string coustomUrl { get; set; }
            /// <summary>
            /// 自定义页面参数名
            /// </summary>
            public string coustomPageParName { get; set; }
            public string WrtiePage()
            {
              
                int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
                if (totalPage>1)
                {
                  
                    StringBuilder strs = new StringBuilder();

                    strs.AppendFormat("<a href=\"{0}{1}={2}\">上一页</a> ", coustomUrl, string.IsNullOrEmpty(coustomPageParName) ? "page" : coustomPageParName, curentPage == 1 ? 1 : curentPage - 1);
                    for (int i = curentPage==1?1:curentPage-1; i <= curentPage+5; i++)
                        {
                            if (i == curentPage)
                            {
                                strs.AppendFormat("<span id='curentPage'>{0}</span> ",i);
                            }
                            else
                            {
                                strs.AppendFormat("<a href=\"{0}{1}={2}\">{2}</a> ", coustomUrl, string.IsNullOrEmpty(coustomPageParName) ? "page" : coustomPageParName, i);

                            }
                            if (i == totalPage || i==totalPage/5) { break; }
                        }
                        strs.AppendFormat("<a href=\"{0}{1}={2}\">下一页</a> ", coustomUrl, string.IsNullOrEmpty(coustomPageParName) ? "page" : coustomPageParName, curentPage == totalPage ? totalPage : curentPage + 1);

                    return   strs.ToString();
                }
                return "";
            }
        }
    }

  • 相关阅读:
    java:IO流(处理流(缓冲流,转换流,数据流),对象的序列化,Properties)
    java:容器/集合(Map(HashMap,TreeMap)Collection和Collections,(Collection-value();)
    java:容器/集合Collection(List(ArrayList,LinkedList,Vector),Set(HashSet(LinkedHashSet),TreeSet))
    java:常用类(包装类,equals和==的比较,Date,java.lang.String中常用方法,枚举enum)
    java:LeakFilling(面向对象)
    java:异常机制(try,catch,finally,throw,throws,自定义异常)
    深入剖析Java中的装箱和拆箱
    Java 如何有效地避免OOM:善于利用软引用和弱引用
    浅谈Java中的hashcode方法
    浅析Java中的访问权限控制
  • 原文地址:https://www.cnblogs.com/fogwang/p/2951417.html
Copyright © 2011-2022 走看看