码迷,mamicode.com
首页 > Windows程序 > 详细

C#分页代码,或许这个代码比Asp.Net分页和AspNetPager控件更好用

时间:2015-10-01 17:50:19      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:

废话不多说,上代码!以下代码比较简单,可供开发扩展,CSS还得各位自己写咯!

 

        /// <summary>
        /// 生成page插件
        /// </summary>
        /// <param name="href">完整连接</param>
        /// <param name="queryParam">参数</param>
        /// <param name="pageindex">当前页面</param>
        /// <param name="pagesize">页面溶剂</param>
        /// <param name="total">条数</param>
        /// <returns></returns>
        public static string GetPages(string href, Dictionary<string, object> queryParam, int pageindex, int pagesize, int total)
        {
            if (pageindex <= 0 || total == 0)
            {
                return "";
            }
            int pt = Convert.ToInt32(Math.Ceiling(total * 1.00 / pagesize));

            if (pt == 1)
            {
                return "";
            }

            string queryStr = "";
            if (queryParam != null)
            {
                foreach (var item in queryParam)
                {
                    queryStr += "&" + item.Key + "=" + item.Value;
                }
            }

            string p = "<div class=‘pagin‘>";
            p += "<div class=‘message‘>共<i class=‘blue‘>" + total + "</i>条记录,当前显示第 <i class=‘blue‘>" + pageindex + " </i>页</div>";
            p += "<ul class=‘paginList‘>";
            if (pageindex > 1)
            {
                p += " <li class=‘paginItem‘><a href=‘" + href + "?page=" + (pageindex - 1) + queryStr + "‘  ><span class=‘pagepre‘ ><</span></a></li>";
            }

            if (total / pagesize < 5)//总页数不超过5
            {
                for (int i = 1; i <= pt; i++)
                {
                    string cl = " class=‘paginItem‘";
                    if (i == pageindex)
                    {
                        cl = " class=‘paginItem current‘";
                    }
                    p += "<li " + cl + " > <a  href=‘" + href + "?page=" + i + queryStr + "‘>" + i + "</a></li>";
                }
            }
            else//总页数超过5
            {
                for (int i = (pageindex - 2) > 0 ? (pageindex - 2) : pageindex; i <= ((pageindex + 3) < pt ? (pageindex + 3) : pt); i++)
                {
                    string cl = " class=‘paginItem‘";
                    if (i == pageindex)
                    {
                        cl = " class=‘paginItem current‘";
                    }
                    p += "<li " + cl + " > <a  href=‘" + href + "?page=" + i + queryStr + "‘>" + i + "</a></li>";
                }
            }

            if (pageindex < pt)
            {
                p += "<li class=‘paginItem‘><a  href=‘" + href + "?page=" + (pageindex + 1) + queryStr + "‘  ><span class=‘pagenxt‘>></span></a></li>";
            }
            p += "</ul>";
            p += "</div>";
            return p;


            // <div class="pagin">
            //    <div class="message">共<i class="blue">1256</i>条记录,当前显示第 <i class="blue">2 </i>页</div>
            //    <ul class="paginList">
            //    <li class="paginItem"><a href="javascript:;"><span class="pagepre"></span></a></li>
            //    <li class="paginItem"><a href="javascript:;">1</a></li>
            //    <li class="paginItem current"><a href="javascript:;">2</a></li>
            //    <li class="paginItem"><a href="javascript:;">3</a></li>
            //    <li class="paginItem"><a href="javascript:;">4</a></li>
            //    <li class="paginItem"><a href="javascript:;">5</a></li>
            //    <li class="paginItem more"><a href="javascript:;">...</a></li>
            //    <li class="paginItem"><a href="javascript:;">10</a></li>
            //    <li class="paginItem"><a href="javascript:;"><span class="pagenxt"></span></a></li>
            //    </ul>
            //</div>
        }

  

欢迎各位大牛小牛支持我的博客,也请各位支持支持我的小站吧---》铜梁视窗

C#分页代码,或许这个代码比Asp.Net分页和AspNetPager控件更好用

标签:

原文地址:http://www.cnblogs.com/daneas/p/4851180.html

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