using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Data.Common;
using System.Data.OleDb;public partial class Array : System.Web.UI.Page
{
    public int min, max;
    public bool check;
    public int indexpage=0;   
    public int pagesize = 10;
    public string strdata="";
    protected void Page_Load(object sender, EventArgs e)
    {
       strdata = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("~/App_Data/db.mdb");     
       if (check!=true)
       {
           datadb("select top " + pagesize + " * from tableTest order by ID desc"); 
           check=true;   
       }
       Response.Write(min);
       Response.Write("<br>");
       Response.Write(max);
    }     
    public void datadb(string sql)
    {
        try
        {                    
            OleDbConnection strcon = new OleDbConnection();
            strcon.ConnectionString = strdata;
            strcon.Open();
            OleDbCommand strcom = new OleDbCommand();
            strcom.Connection = strcon;
            strcom.CommandText = sql;
            OleDbDataReader dr = strcom.ExecuteReader();
            if(dr.Read())
            {
            int i = 0;
            int[] rr = new int[10];
            while (i<pagesize)
            {
                rr[i] = Convert.ToInt32(dr["id"]);
                i++;
            }
            min=rr[9];
            max=rr[0]; 
            pageList1.DataSource = dr;
            pageList1.DataBind();
            }
            //OleDbDataAdapter adapter = new OleDbDataAdapter();
            //adapter.SelectCommand=strcom;
            //DataSet ds = new DataSet();
            //DataTable dt = new DataTable();
            //adapter.Fill(ds,"employees");          
            //dt = ds.Tables[0];
            //pageList1.DataSource=ds.Tables[0].DefaultView;
            //pageList1.DataBind();
            //string tempStr="";
            //foreach (DataRow tempRow in dt.Rows)
            //{
            //    tempStr +=tempRow["ID"].ToString() + ","; //这里就是每行的ID号了
            //}         
            //string[] rr ={tempStr};
            //min = Convert.ToInt32(ds.Tables[0].Rows[9][0]);
            //max = Convert.ToInt32(ds.Tables[0].Rows[0][0]);       
            strcon.Close();
            strcon.Dispose();            
            }
        catch (Exception ms)
            {
                Response.Write(ms.Message);
            }             
    }
    protected int numbers() 
    {//计算总记录
        OleDbConnection ncon = new OleDbConnection();
        ncon.ConnectionString = strdata;
        ncon.Open();
        OleDbCommand ncom = new OleDbCommand( "select Count(*) from tableTest",ncon);
        Int32 allNum = Convert.ToInt32(ncom.ExecuteScalar());  
        ncon.Close();
        ncon.Dispose();
        return allNum;
    }
    public int page() 
    {//总页数
        int allcount = numbers();//数据总量 
        int allpage;
        allpage = allcount / pagesize;
        if (allcount % pagesize != 0)
            allpage += 1;       
        return allpage;        
    }
    public int remainder() 
    {
        int allcount = numbers();
        int remainder;
        remainder = (int)(allcount % pagesize);
        return remainder; 
    }   
   /* public void reckon(string pagenum)
    {
        OleDbConnection con = new OleDbConnection();
        con.Open();
        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandText = pagenum;
        cmd.Connection = con;
        OleDbDataAdapter DS = new OleDbDataAdapter();
        DS.SelectCommand = cmd;
        DataSet dr = new DataSet();
        DataTable tb = new DataTable();
        DS.Fill(dr,"table");
        tb=dr.Tables[0];
        indexnum=dr.Tables[0].Roms[][0];
    }*/
    //public int row()
    //{
    //    int numall;
    //    numall = pagesize * indexpage;
    //    return numall;
    //}
    protected void nextLinkButton2_Click(object sender, EventArgs e)
    {
        if (check == true)
        {
            datadb("select top " + pagesize + " * from tableTest where ID < " + min + " order by ID desc");
            check=true;
        }    
    }
    protected void upLinkButton3_Click(object sender, EventArgs e)
    {
        if (check == true)
        {
            datadb("select top " + pagesize + " * from tableTest where ID > " + max + " order by ID desc");
            check = true;
        }     
    }
    protected void lastLinkButton4_Click(object sender, EventArgs e)
    {
        datadb("select top " + remainder() + " * from tableTest order by ID asc");
    }
    protected void firstLinkButton1_Click(object sender, EventArgs e)
    {       
        datadb("select top " + pagesize + " * from tableTest order by ID desc");       
    } 
}
不知道那里有问题只能分一页就不动了!
希望高手能帮我改好!

解决方案 »

  1.   

    if(!IsPostBack)判断是否回发
     protected void Page_Load(object sender, EventArgs e) 
        { 
    if(!IsPostBack){
          strdata = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("~/App_Data/db.mdb");    
          if (check!=true) 
          { 
              datadb("select top " + pagesize + " * from tableTest order by ID desc"); 
              check=true;  
          } 
    }
          Response.Write(min); 
          Response.Write(" <br>"); 
          Response.Write(max); 
        }    另外
    最后一页不能这样取,还需要重新排序  protected void lastLinkButton4_Click(object sender, EventArgs e) 
        { 
            datadb("select top " + remainder() + " * from tableTest order by ID asc"); 
        }
      

  2.   

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Text;
    using System.Data.Common;
    using System.Data.OleDb;public partial class Array : System.Web.UI.Page
    {
        public int min, max;    
        public int indexpage=0;
        public bool check=false;
        public int pagesize = 10;
        public string strdata="";
        protected void Page_Load(object sender, EventArgs e)
        {        
           strdata = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("~/App_Data/db.mdb");
           if (!IsPostBack)
           {         
                   datadb("select top " + pagesize + " * from tableTest order by ID desc");
           }
               
           Response.Write(min);
           Response.Write("<br>");
           Response.Write(max);
        }     
        public void datadb(string sql)
        {
            try
            {                    
                OleDbConnection strcon = new OleDbConnection();
                strcon.ConnectionString = strdata;
                strcon.Open();
                OleDbCommand strcom = new OleDbCommand();
                strcom.Connection = strcon;
                strcom.CommandText = sql;    
                OleDbDataAdapter adapter = new OleDbDataAdapter();
                adapter.SelectCommand = strcom;
                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                adapter.Fill(ds, "employees");
                dt = ds.Tables[0];
                pageList1.DataSource = ds.Tables[0].DefaultView;
                pageList1.DataBind();
               min = Convert.ToInt32(ds.Tables[0].Rows[9][0]);
                max = Convert.ToInt32(ds.Tables[0].Rows[0][0]);       
                strcon.Close();
                strcon.Dispose();            
                }
            catch (Exception ms)
                {
                    Response.Write(ms.Message);
                }             
        }
        protected int numbers() 
        {//计算总记录
            OleDbConnection ncon = new OleDbConnection();
            ncon.ConnectionString = strdata;
            ncon.Open();
            OleDbCommand ncom = new OleDbCommand( "select Count(*) from tableTest",ncon);
            Int32 allNum = Convert.ToInt32(ncom.ExecuteScalar());  
            ncon.Close();
            ncon.Dispose();
            return allNum;
        }
        public int page() 
        {//总页数
            int allcount = numbers();//数据总量 
            int allpage;
            allpage = allcount / pagesize;
            if (allcount % pagesize != 0)
                allpage += 1;       
            return allpage;        
        }
        public int remainder() 
        {
            int allcount = numbers();
            int remainder;
            remainder = (int)(allcount % pagesize);
            return remainder; 
        }      
        protected void nextLinkButton2_Click(object sender, EventArgs e)
        {
            datadb("select top " + pagesize + " * from tableTest where ID < " + min + "order by ID desc");          
              
        }
        protected void upLinkButton3_Click(object sender, EventArgs e)
        {
            datadb("select top " + pagesize + " * from tableTest where ID > " + max + " order by ID desc");   
        }
        protected void lastLinkButton4_Click(object sender, EventArgs e)
        {
            datadb("select top " + remainder() + " * from tableTest order by ID asc");
        }
        protected void firstLinkButton1_Click(object sender, EventArgs e)
        {       
            datadb("select top " + pagesize + " * from tableTest order by ID desc");       
        } 
    }
    这样也不行啊!
    那位高手帮我弄好了加分也不成问题啊!