public string pageIndex()
    {
        int num = 0;//当前页码
        if (string.IsNullOrWhiteSpace(Request["page"]) || Convert.ToInt32(Request["page"]) <= 1)
        {
            num = 1;
        }
        else
        {
            num = Convert.ToInt32(Request["page"]) ;
        }        StringBuilder sb = new StringBuilder();
        con.Open();
        string sql = "select count(id) from aa";
        SqlCommand com = new SqlCommand(sql, con);
        int count = Convert.ToInt32(com.ExecuteScalar());
        con.Close();
        int page = 0;//页总数
        page =(int)Math.Ceiling((decimal)count / 20 );
        int from = num / 10 + 1;
        int to = num / 10 + 11;
        for (int i = from; i < to && i < page; i++)
        {
            if (i == num)
                sb.AppendLine("<strong>"+i+"</strong>");
            else
                sb.AppendLine("<a href='?page=" + i  + "'>" + i + "</a>");
        }
        if (to - from >= 10)
        {
            sb.AppendLine("<a href='?page=" + (from + 10) + "'>...</a>");
        }
        if (num <= page)
        {
            sb.AppendLine("<a href='?page=" + (num +1) + "'>下一页</a>");
        }
            return sb.ToString();
    }
问题出在from和to赋值上
当 点击的时候 当前页码变成url路径页码变成11
在计算 from的时候就会出现问题 int from =11/10+1这里居然不是11起始。。
更坑爹的是to这个时候是是12 
怎么办呢

解决方案 »

  1.   

    是不是这个是关键字的问题?你换一个变量名试试呢?我刚转WEB 刚坐的一个项目用的是那个aspnetpager 你这个是后台拼接 前台用JQ JS 来操作的- -?
      

  2.   

    int pageSize=20 
    int page =  ceiling(1.0*count/pageSize)
    int from  = (num - 1) * pageSize +1
    int to = from + pageSize -1 
      

  3.   

    int from =11/10+1这里居然不是11起始。。
    更坑爹的是to这个时候是是12  
    怎么办呢-------------------------
    传过11当然不是11起始  int from = num / 10 + 1;
            int to = num / 10 + 11;
    from是2  to是12  当然是这样的结果   
    改下  int from=num/12+11;  //这样不就11为起始页了么   不知道你to想为什么值  自己试着改改上面这两句
      

  4.   

    int pageSize=20
    是什么意思?
      

  5.   

    用aspnetpager 吧 int from =11/10+1怎么能算出来11?
      

  6.   


    pageSize 分页大小,每页显示的数据量
      

  7.   

    不好意思,语句是从sql存储过程里拿来的,所以有ceiling这种关键字,思路没问题的。
      

  8.   

    nt pageSize=20;//分页数量int page = count/pageSize;
    if(count%pageSize>0)page++;//计算总页数int from = (num - 1) * pageSize +1;//计算起始位置
    int to = from + pageSize -1;//终止位置我测试过的,可能,你没写好吧。
      

  9.   

    另外,之前总结的一些分页方法,你可以看一下。
    http://blog.csdn.net/wszhoho/article/category/299268
      

  10.   

    赋值粘贴修改
    pagesize=20 num是url页码 每次都成倍增长。
      

  11.   


     public string pageIndex()
        {
            int num = 0;//当前页码
            if (string.IsNullOrWhiteSpace(Request["page"]) || Convert.ToInt32(Request["page"]) <= 1)
            {
                num = 1;
            }
            else
            {
                num = Convert.ToInt32(Request["page"]) ;
            }        StringBuilder sb = new StringBuilder();
            con.Open();
            string sql = "select count(id) from aa";
            SqlCommand com = new SqlCommand(sql, con);
            int count = Convert.ToInt32(com.ExecuteScalar());
            con.Close();
            int page = 0;//页总数
            page =(int)Math.Ceiling((decimal)count / 20 );
            int from = (num/10)*10 + 1;
            int to =  from+10;if(to>page){
    to=page;
    }
            for (int i = from; i < to && i < page; i++)
            {
                if (i == num)
                    sb.AppendLine("<strong>"+i+"</strong>");
                else
                    sb.AppendLine("<a href='?page=" + i  + "'>" + i + "</a>");
            }
            if (to - from >= 10)
            {
                sb.AppendLine("<a href='?page=" + to + "'>...</a>");
            }
            if (num <= page)
            {
                sb.AppendLine("<a href='?page=" + (num +1) + "'>下一页</a>");
            }
                return sb.ToString();
        }
      

  12.   

    int from = (num/10)*10 + 1; // 当前分段的起始页码
    int to =  from+10; //当前分段的终止页码// 如果终止页码大于总页数,则当前分段的终止页码为总页数
    if(to>page){
      to=page;
    }
      

  13.   

     /// <summary>
                /// 分页导航菜单 {0} 代表页码 {2}代表搜索关键字
                /// </summary>
                /// <param name="_AllPage">总页码</param>
                /// <param name="NowPage">索引页码</param>
                /// <param name="SearchKey">搜索关键字</param>
                /// <returns></returns>
                public static String MenuUrl(String TempLate, int AllPage, int NowPage, int PageLength, String SearchKey)
                {
                    TempLate = TempLate.Replace("{2}", SearchKey);
                    StringBuilder StrPage = new StringBuilder();
                    StrPage.Append("当前页&nbsp;");
                    if (NowPage < 1)
                    {
                        NowPage = 1;
                    }
                    StrPage.Append(NowPage.ToString() + "/" + AllPage.ToString());
                    StrPage.Append("<a href=\"" + String.Format(TempLate, 1) + "\" class=\"MenuArticle\">&laquo; 首页</a>");
                    StrPage.Append((NowPage <= 1 ? "<a  class=\"disabled\">&laquo; 上一页 </a>" : "<a href=\"" + String.Format(TempLate, (NowPage - 1).ToString()) + "\" class=\"MenuArticle\">&laquo; 上一页</a>"));
                    int StartI = (NowPage - PageLength) > 0 ? (NowPage - PageLength) : 1;
                    int EndI = (NowPage + PageLength) < AllPage ? (NowPage + PageLength) : AllPage;
                    for (int i = StartI; i <= EndI; i++)
                    {
                        if (i == NowPage)
                        {
                            StrPage.Append("<a  class=\"number current\">" + i.ToString() + "</a>");
                        }
                        else
                        {
                            StrPage.Append("<a href=\"" + String.Format(TempLate, i.ToString()) + "\" class=\"number\">" + i.ToString() + "</a>");
                        }
                    }
                    StrPage.Append((NowPage >= AllPage ? "<a  class=\"disabled\"> 下一页  &raquo;</a>" : "<a href=\"" + String.Format(TempLate, (NowPage + 1).ToString()) + "\" class=\"MenuArticle\">下一页</a>"));
                    StrPage.Append("<a href=\"" + String.Format(TempLate, AllPage.ToString()) + "\" class=\"MenuArticle\">末页 &raquo;</a>");
                    return StrPage.ToString();
                }
      

  14.   

    上边不对: public string pageIndex()
        {
            int num = 0;//当前页码
            if (string.IsNullOrWhiteSpace(Request["page"]) || Convert.ToInt32(Request["page"]) <= 1)
            {
                num = 1;
            }
            else
            {
                num = Convert.ToInt32(Request["page"]) ;
            }        StringBuilder sb = new StringBuilder();
            con.Open();
            string sql = "select count(id) from aa";
            SqlCommand com = new SqlCommand(sql, con);
            int count = Convert.ToInt32(com.ExecuteScalar());
            con.Close();
            int page = 0;//页总数
            page =(int)Math.Ceiling((decimal)count / 20 );
            int from = (num/10)*10 + 1; // 当前分段的起始页码
            int to =  from+9; //当前分段的终止页码// 如果终止页码大于总页数,则当前分段的终止页码为总页数
    if(to>page){
    to=page;
    }/ 前一个分段
            if (from>10)
            {
                sb.AppendLine("<a href='?page=" + (from-1) + "'>...</a>");
            }//显示分段页码
            for (int i = from; i <= to && i <= page; i++)
            {
                if (i == num)
                    sb.AppendLine("<strong>"+i+"</strong>");
                else
                    sb.AppendLine("<a href='?page=" + i  + "'>" + i + "</a>");
            }// 后一个分段
            if (to<page)
            {
                sb.AppendLine("<a href='?page=" + (to+1) + "'>...</a>");
            }        if (num <= page)
            {
                sb.AppendLine("<a href='?page=" + (num +1) + "'>下一页</a>");
            }
                return sb.ToString();
        }
      

  15.   

    / 前一个分段
            if (from>10)
            {
                sb.AppendLine("<a href='?page=" + (from-1) + "'>...</a>");
            }这个还不对,楼主去掉这个吧。
      

  16.   


    // 前一个分段应这样算
    if (from>10)
    {
      sb.AppendLine("<a href='?page=" + ((from-10)/10)*10+1) + "'>...</a>");
    }
      

  17.   

    现在这些问题都解决了 剩下一个问题当前页面极值处理 也就是说当前点击10的时候他就做新的分页分组了
        public string pageIndex()
        {
            int num = 0;//当前页码
            if (string.IsNullOrWhiteSpace(Request["page"]) || Convert.ToInt32(Request["page"]) <= 1)
            {
                num = 1;
            }
            else
            {
                num = Convert.ToInt32(Request["page"]) ;
            }        StringBuilder sb = new StringBuilder();
            con.Open();
            string sql = "select count(id) from aa";
            SqlCommand com = new SqlCommand(sql, con);
            int count = Convert.ToInt32(com.ExecuteScalar());
            con.Close();
            int page = 0;//页总数
            page = (int)Math.Ceiling((decimal) count / 20);
            int from = num / 10 * 10 + 1;//起始页
            int to = num / 10 * 10 + 11;//终止页        for (int i = from; i < to && i < page; i++)
            {
                if (i == num)
                    sb.AppendLine("<strong>"+i+"</strong>");
                else
                    sb.AppendLine("<a href='?page=" + i  + "'>" + i + "</a>");
            }
            if (to - from >= 10)
            {
                sb.AppendLine("<a href='?page=" + (from + 10) + "'>...</a>");
            }
            if (num <= page)
            {
                sb.AppendLine("<a href='?page=" + (num +1) + "'>下一页</a>");
            }
                return sb.ToString();
        }
      

  18.   


    还要加一个判断:int from = (num/10)*10 + 1;
    if(from%10==0&from>=10){
    from=(num/10-1)*10 + 1;
    }