我在网上搜到的一段分页代码,不过使用vs2008写的,里面的好多东西不能用在vs2005,如命名空间、还有数据库连接等。
我想把他改成一个完全可以在vs2005下运行的代码。(改完以后直接复制能使用)
源代码如下:using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class pagination : System.Web.UI.Page
{
    public int PageSize = 10; //设置每页显示多少条记录
    protected void Page_Load(object sender, EventArgs e)
    {                  
        //建立连接
        SqlConnection conn = DB.Connection();
        conn.Open();
        //查看是否已经连接上   Response.Write(conn.State.ToString());
        string strSQL = "select count(*) from pagination ";
        SqlCommand cmd = new SqlCommand(strSQL, conn);
        int MaxSize = Convert.ToInt32(cmd.ExecuteScalar()); //取出总笔数 
        int Page = Convert.ToInt32(Request.QueryString.Get("PageID")); //获取页码
        if (Page == 0)
        {
            Page = 1;
        }
        float temp = (float)MaxSize / PageSize;
        if (Page >= Convert.ToInt32(Math.Ceiling(temp)))
        {
            Page = Convert.ToInt32(Math.Ceiling(temp));
        }
        string SQL = "select top "+PageSize+" * from pagination where UserID>" + (Page - 1) * PageSize;
        SqlCommand scmd = new SqlCommand(SQL,conn);
        SqlDataReader sdr = scmd.ExecuteReader();
        string html = "<table class=table border=1>";
                html += "<tr>";
                html += "<td width=150px>用 户 名</td>";
                html += "<td width=100px>密 码</td>";
                html += "<td width=250px>地 址</td>";
                html += "</tr>";
        while (sdr.Read())
        {            
            html += "<tr>";
            html += "<td width=150px>" + sdr["UserName"].ToString() + "</td>";
            html += "<td width=100px>" + sdr["UserPwd"].ToString() + "</td>";
            html += "<td width=250px>" + sdr["UserAddr"].ToString() + "</td>";
            html += "</tr>";
        }
           html += "</table>";        //关闭连接 
        sdr.Close();   
        conn.Close();
        show.Text = html;
        LblTitle.Text = "ASP.NET分页代码……共有" + Convert.ToInt32(Math.Ceiling(temp)) + "页/当前是第" + Page + "页";
       
    }
    protected void BtnFirst_Click(object sender, EventArgs e)
    {
        Response.Redirect("pagination.aspx?PageID=1");
    }
    protected void BtnPre_Click(object sender, EventArgs e)
    {
        int Page = Convert.ToInt32(Request.QueryString.Get("PageID"));
        if (Page <= 1)
        {
            Page = 2;
        }
        int curent = Page - 1;
        Response.Redirect("pagination.aspx?PageID=" + curent);
    }
    protected void BtnNext_Click(object sender, EventArgs e)
    {
        SqlConnection conn = DB.Connection();
        conn.Open(); string strSQL = "select count(*) from pagination ";
        SqlCommand cmd = new SqlCommand(strSQL, conn);
        int MaxSize = Convert.ToInt32(cmd.ExecuteScalar()); //取出总笔数 
        int Page = Convert.ToInt32(Request.QueryString.Get("PageID"));
        float temp = (float)MaxSize / PageSize;      
        if (Page >= Convert.ToInt32(Math.Ceiling(temp)))
        {
            Page = Convert.ToInt32(Math.Ceiling(temp))-1;
        }
        if (Page <= 0)
        {
            Page = 1;
        }
        int curent = Page + 1;
        Response.Redirect("pagination.aspx?PageID=" + curent);
        conn.Close();
    }
    protected void BtnLast_Click(object sender, EventArgs e)
    {
        SqlConnection conn = DB.Connection();
        conn.Open();
        string strSQL = "select count(*) from pagination ";
        SqlCommand cmd = new SqlCommand(strSQL, conn);
        int MaxSize = Convert.ToInt32(cmd.ExecuteScalar()); //取出总笔数 
        float temp = (float)MaxSize / PageSize;
        Response.Redirect("pagination.aspx?PageID=" + Math.Ceiling(temp));
        conn.Close();
    } 
}

解决方案 »

  1.   

    分页代码很多呀,为什么一定要用这个2008的?
    http://blog.csdn.net/46539492/archive/2008/04/11/2283360.aspx
    http://blog.csdn.net/46539492/archive/2008/04/02/2244627.aspx
      

  2.   

    去掉System.Linq, System.Xml.Linq两个命名空间就可以了DB.Connection()是什么?
      

  3.   

    AspNetPager使用最广泛的分页控件
    http://www.webdiyer.com/AspNetPager/default.aspx
      

  4.   

    去掉linq引用,把sql连接换成你自己的,去掉页面控件label,编译能通过
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Data.SqlClient;
    public partial class pagination : System.Web.UI.Page
    {
        public int PageSize = 10; //设置每页显示多少条记录
        protected void Page_Load(object sender, EventArgs e)
        {
            //建立连接
            //SqlConnection conn =  DB.Connection();
            SqlConnection conn = new SqlConnection("server=.;database=xx;uid=xx;pwd=xx");
            conn.Open();
            //查看是否已经连接上   Response.Write(conn.State.ToString());
            string strSQL = "select count(*) from pagination ";
            SqlCommand cmd = new SqlCommand(strSQL, conn);
            int MaxSize = Convert.ToInt32(cmd.ExecuteScalar()); //取出总笔数 
            int Page = Convert.ToInt32(Request.QueryString.Get("PageID")); //获取页码
            if (Page == 0)
            {
                Page = 1;
            }
            float temp = (float)MaxSize / PageSize;
            if (Page >= Convert.ToInt32(Math.Ceiling(temp)))
            {
                Page = Convert.ToInt32(Math.Ceiling(temp));
            }
            string SQL = "select top " + PageSize + " * from pagination where UserID>" + (Page - 1) * PageSize;
            SqlCommand scmd = new SqlCommand(SQL, conn);
            SqlDataReader sdr = scmd.ExecuteReader();
            string html = "<table class=table border=1>";
            html += "<tr>";
            html += "<td width=150px>用 户 名</td>";
            html += "<td width=100px>密 码</td>";
            html += "<td width=250px>地 址</td>";
            html += "</tr>";
            while (sdr.Read())
            {
                html += "<tr>";
                html += "<td width=150px>" + sdr["UserName"].ToString() + "</td>";
                html += "<td width=100px>" + sdr["UserPwd"].ToString() + "</td>";
                html += "<td width=250px>" + sdr["UserAddr"].ToString() + "</td>";
                html += "</tr>";
            }
            html += "</table>";        //关闭连接 
            sdr.Close();
            conn.Close();        //页面上的控件先注释
            //show.Text = html;
            //LblTitle.Text = "ASP.NET分页代码……共有" + Convert.ToInt32(Math.Ceiling(temp)) + "页/当前是第" + Page + "页";    }
        protected void BtnFirst_Click(object sender, EventArgs e)
        {
            Response.Redirect("pagination.aspx?PageID=1");
        }
        protected void BtnPre_Click(object sender, EventArgs e)
        {
            int Page = Convert.ToInt32(Request.QueryString.Get("PageID"));
            if (Page <= 1)
            {
                Page = 2;
            }
            int curent = Page - 1;
            Response.Redirect("pagination.aspx?PageID=" + curent);
        }
        protected void BtnNext_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("server=.;database=xx;uid=xx;pwd=xx");
            conn.Open(); string strSQL = "select count(*) from pagination ";
            SqlCommand cmd = new SqlCommand(strSQL, conn);
            int MaxSize = Convert.ToInt32(cmd.ExecuteScalar()); //取出总笔数 
            int Page = Convert.ToInt32(Request.QueryString.Get("PageID"));
            float temp = (float)MaxSize / PageSize;
            if (Page >= Convert.ToInt32(Math.Ceiling(temp)))
            {
                Page = Convert.ToInt32(Math.Ceiling(temp)) - 1;
            }
            if (Page <= 0)
            {
                Page = 1;
            }
            int curent = Page + 1;
            Response.Redirect("pagination.aspx?PageID=" + curent);
            conn.Close();
        }
        protected void BtnLast_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("server=.;database=xx;uid=xx;pwd=xx");
            conn.Open();
            string strSQL = "select count(*) from pagination ";
            SqlCommand cmd = new SqlCommand(strSQL, conn);
            int MaxSize = Convert.ToInt32(cmd.ExecuteScalar()); //取出总笔数 
            float temp = (float)MaxSize / PageSize;
            Response.Redirect("pagination.aspx?PageID=" + Math.Ceiling(temp));
            conn.Close();
        }
    }
      

  5.   

    userid是连续的么,不连续用下面的就好了
    string SQL = "select top "+PageSize.ToString()+" * from pagination where UserID not in (select top " + ((Page - 1) * PageSize).ToString() + " userid from pagination )";
      

  6.   

    不行啊。楼上的方法还是运行不了 有三个错误提示:1。“ASP.index_aspx.GetTypeHashCode()”: 没有找到适合的方法来重写2。“ASP.index_aspx.ProcessRequest(System.Web.HttpContext)”: 没有找到适合的方法来重写3.“ASP.index_aspx”不会实现接口成员“System.Web.IHttpHandler.IsReusable”
      

  7.   

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Data.SqlClient;
    public partial class pagination : System.Web.UI.Page
    {
        public int PageSize = 10; //设置每页显示多少条记录
        protected void Page_Load(object sender, EventArgs e)
        {                  
            //建立连接
    string StrConn=ConfigurationManager.AppSetting["ConnectionString"];
            SqlConnection conn = new SqlConnection(StrConn);
            conn.Open();
            //查看是否已经连接上   Response.Write(conn.State.ToString());
            string strSQL = "select count(*) from pagination ";
            SqlCommand cmd = new SqlCommand(strSQL, conn);
            int MaxSize = Convert.ToInt32(cmd.ExecuteScalar()); //取出总笔数 
            int Page = Convert.ToInt32(Request.QueryString.Get("PageID")); //获取页码
            if (Page == 0)
            {
                Page = 1;
            }
            float temp = (float)MaxSize / PageSize;
            if (Page >= Convert.ToInt32(Math.Ceiling(temp)))
            {
                Page = Convert.ToInt32(Math.Ceiling(temp));
            }
            string SQL = "select top "+PageSize+" * from pagination where UserID>" + (Page - 1) * PageSize;
            SqlCommand scmd = new SqlCommand(SQL,conn);
            SqlDataReader sdr = scmd.ExecuteReader();
            string html = "<table class=table border=1>";
                    html += "<tr>";
                    html += "<td width=150px>用 户 名</td>";
                    html += "<td width=100px>密 码</td>";
                    html += "<td width=250px>地 址</td>";
                    html += "</tr>";
            while (sdr.Read())
            {            
                html += "<tr>";
                html += "<td width=150px>" + sdr["UserName"].ToString() + "</td>";
                html += "<td width=100px>" + sdr["UserPwd"].ToString() + "</td>";
                html += "<td width=250px>" + sdr["UserAddr"].ToString() + "</td>";
                html += "</tr>";
            }
               html += "</table>";        //关闭连接 
            sdr.Close();   
            conn.Close();
            show.Text = html;
            LblTitle.Text = "ASP.NET分页代码……共有" + Convert.ToInt32(Math.Ceiling(temp)) + "页/当前是第" + Page + "页";
           
        }
        protected void BtnFirst_Click(object sender, EventArgs e)
        {
            Response.Redirect("pagination.aspx?PageID=1");
        }
        protected void BtnPre_Click(object sender, EventArgs e)
        {
            int Page = Convert.ToInt32(Request.QueryString.Get("PageID"));
            if (Page <= 1)
            {
                Page = 2;
            }
            int curent = Page - 1;
            Response.Redirect("pagination.aspx?PageID=" + curent);
        }
        protected void BtnNext_Click(object sender, EventArgs e)
        {
            SqlConnection conn = DB.Connection();
            conn.Open(); string strSQL = "select count(*) from pagination ";
            SqlCommand cmd = new SqlCommand(strSQL, conn);
            int MaxSize = Convert.ToInt32(cmd.ExecuteScalar()); //取出总笔数 
            int Page = Convert.ToInt32(Request.QueryString.Get("PageID"));
            float temp = (float)MaxSize / PageSize;      
            if (Page >= Convert.ToInt32(Math.Ceiling(temp)))
            {
                Page = Convert.ToInt32(Math.Ceiling(temp))-1;
            }
            if (Page <= 0)
            {
                Page = 1;
            }
            int curent = Page + 1;
            Response.Redirect("pagination.aspx?PageID=" + curent);
            conn.Close();
        }
        protected void BtnLast_Click(object sender, EventArgs e)
        {
            SqlConnection conn = DB.Connection();
            conn.Open();
            string strSQL = "select count(*) from pagination ";
            SqlCommand cmd = new SqlCommand(strSQL, conn);
            int MaxSize = Convert.ToInt32(cmd.ExecuteScalar()); //取出总笔数 
            float temp = (float)MaxSize / PageSize;
            Response.Redirect("pagination.aspx?PageID=" + Math.Ceiling(temp));
            conn.Close();
        } 
    }
      

  8.   


    1。“ASP.index_aspx.GetTypeHashCode()”: 没有找到适合的方法来重写 2。“ASP.index_aspx.ProcessRequest(System.Web.HttpContext)”: 没有找到适合的方法来重写 3.“ASP.index_aspx”不会实现接口成员“System.Web.IHttpHandler.IsReusable”
      

  9.   

    新建一个页面,名字就叫pagination.aspx.把代码复制到.cs文件.
      

  10.   

    上一页,下一页应该要自己拖button的.并且把OnClick设为对应的事件.
    如上一页 OnClick="BtnPre_Click"