using System;
using System.IO;
using System.Web;namespace MyPage
{
  public class Pager
  {
 
    public Pager(){}       public string setcolor(int i,string color1,string color2)
    {
      if (i % 2 == 0) { 
       return color1; 
      } else { 
       return color2; 
      }
    }        //判断第几页
    public int DealPageNo(string PageNo)
    {
      if (PageNo!= null) { 
       return 1; 
      } else { 
       return Convert.ToInt32(PageNo); 
      }
    }
    
    //SQL = PageSQL(pageNo,PageSize,"Article","*","ID","CLASSID = " & request("classid"),"addtime desc")
    public string PageSQL(string PageNo,string PageSize,string Table,string Word,string Key,string where,string OrderBy)
    {
    
      string  tempSql,strSelect1,strSelect2,strSelect3;
      int iPageNo;
      int iPageSize;
      tempSql = ""; 
      
      iPageNo = DealPageNo(PageNo);
      iPageSize = Convert.ToInt32(PageSize);
      if ((iPageNo == 1)) { 
       tempSql = "select top " + iPageSize + " " + Word + " from " + Table; 
       if ((where != "")) { 
         tempSql = tempSql + " where " + where; 
       } 
       if ((OrderBy != "")) { 
         tempSql = tempSql + " order by " + OrderBy; 
       } 
      } else { 
       strSelect1 = "select top " + iPageSize + " " + Word + " from " + Table + " where " + Key + " NOT IN ("; 
       strSelect2 = "select top " + iPageSize * (iPageNo - 1) + " " + Key + " from " + Table; 
       strSelect3 = ") "; 
       if ((where != "")) { 
         strSelect2 = strSelect2 + " where " + where; 
         strSelect3 = strSelect3 + " and (" + where + ")"; 
       } 
       if ((OrderBy != "")) { 
         strSelect2 = strSelect2 + " order by " + OrderBy; 
         strSelect3 = strSelect3 + " order by " + OrderBy; 
       } 
       tempSql = strSelect1 + strSelect2 + strSelect3; 
      } 
      return  tempSql;
    }
      
    public string CountSql(string Table,string Where)
    {
      string tempSql; 
      tempSql = "select count(*) from " + Table; 
      if ((Where != "")) { 
        tempSql = tempSql + " where " + Where; 
      } 
      return tempSql;  
    }
         
    public string pageing_msg(int Count,int pageSize,int pageNo)
    {
       string s = "";
       int PageCount = 0;
            
       PageCount = Convert.ToInt32((Count + pageSize - 1) / pageSize); 
       s = "共有<b><font color=\"#990000\">" + Count + "</font></b>条信息"; 
       s = s + " 分<b><font color=\"#990000\">" + PageCount + "</font></b>页"; 
       s = s + " 当前页:<b><font color=\"#990000\">" + pageNo + "</font></b>";
       return s;
    }
  }    
}

解决方案 »

  1.   

    楼主贯彻了ASP函数的传统形式,建议将它对象化。
      

  2.   

    这个代码把分页的逻辑和sql语句都混到一起了,在.net里一般不这样写吧
    我自己写的控件只包含分页的逻辑,数据来源是放在数据层里做的,返回dataset
    而显示分页的样式则是用用户控件,好处是可以随意定义外表,现在流行嘛,呵呵
      

  3.   

    <asp:Label ID="Current" runat="server" Text=""></asp:Label>
                        <asp:Label ID="PageCount"
                            runat="server" Text=""></asp:Label>
                        <asp:Label ID="RecordCount" runat="server"
                                Text=""></asp:Label>
                        <asp:LinkButton ID="btnFist" runat="server" CommandArgument="Fist" OnClick="PageChange">首页</asp:LinkButton>
                        <asp:LinkButton ID="btnPrev" runat="server" CommandArgument="Prev" OnClick="PageChange">上一页</asp:LinkButton>
                        <asp:LinkButton ID="btnNext" runat="server" CommandArgument="Next" OnClick="PageChange">下一页</asp:LinkButton>
                        <asp:LinkButton ID="btnLast" runat="server" CommandArgument="Last" OnClick="PageChange">尾页</asp:LinkButton>
                        跳转<input id="txtNum" style="width: 24px" type="text" runat="server" />页
                        <asp:Button ID="btnGo" runat="server" Text="Go" OnClick="btnGo_Click" />asp.cs:
     public void PageChange(object sender, EventArgs e)//分页
        {
            string direction = ((LinkButton)sender).CommandArgument;
            switch (direction)
            {
                case "Fist":
                    GridView1.PageIndex = 0;
                    break;
                case "Prev":
                    GridView1.PageIndex =Math.Max(GridView1.PageIndex - 1, 0);
                    break;
                case "Next":
                    GridView1.PageIndex =Math.Min(GridView1.PageIndex + 1,GridView1.PageCount - 1);
                    break;
                case "Last":
                    GridView1.PageIndex = GridView1.PageCount - 1;
                    break;
                default:
                    break;
            }
            GetDataBind();
        }
    private void GetDataBind()//绑定
        {
            BLL bll=new BLL ();
            GridView1.DataSource = bll.GetBookTable().DefaultView;
            GridView1.DataBind();
            this.Current.Text = "第" + (GridView1.PageIndex + 1).ToString() + "页";
            this.PageCount.Text = "共" + GridView1.PageCount.ToString() + "页";
            this.RecordCount.Text = "总共" + bll.GetBookTable().Rows.Count.ToString() + "条记录";
            if (GridView1.PageIndex == 0)
            {
                btnFist.Visible = false;
                btnPrev.Visible = false;
            }
            else
            {
                btnFist.Visible = true;
                btnPrev.Visible = true;
            }
            if (GridView1.PageIndex == GridView1.PageCount - 1)
            {
                btnNext.Visible = false;
                btnLast.Visible = false;
            }
            else 
            {
                btnNext.Visible = true;
                btnLast.Visible = true;
            }    }
     protected void btnGo_Click(object sender, EventArgs e)//跳转
        {
            if (Int32.Parse(txtNum.Value.Trim()) <= GridView1.PageCount)
            {
                GridView1.PageIndex = (Convert.ToInt32(txtNum.Value.Trim()) - 1);
            }
            else
            {
                RegisterStartupScript("", "<script>alert('页码不能大于总页数')</script>");
            }
            GetDataBind();
        }
      

  4.   

    //判断第几页
        public int DealPageNo(string PageNo)
        {
          if (PageNo!= null) { 
           return 1; 
          } else { 
           return Convert.ToInt32(PageNo); 
          }
        }=================
    没有考虑到非数字,或者小于等于0
      

  5.   

    PageCount = Convert.ToInt32((Count + pageSize - 1) / pageSize);
    用来计算又多少页,严重错误,没有考虑Int数据类型自己四舍五入。
    有时候漏算一页,多一页 /// <summary>
    /// 计算分页--进一法
    /// </summary>
    /// <param name="TotalNo">有N行数据</param>
    /// <param name="PageSize">每页显示N行</param>
    /// <returns>分页后有X页</returns>
    public static int CutPage(double TotalNo, double PageSize) 

    return (int)Math.Ceiling(TotalNo / PageSize); 
    }
      

  6.   

    http://community.csdn.net/Expert/topic/5084/5084788.xml?temp=.8778803
    刚刚发的免费控件,用.net编程的话使用高效率的控件比较快,这是我的想法