如题

解决方案 »

  1.   


    ASP.NET2.0中datalist仿百度分页(和google差不多)
    我本人做的时候用的是MYSQL数据库, 测试时请转化到MSSQL数据库即可
    注意此方法在ASP.NET1.0或1.1中测试通不过,但是基本方法是没问题,关键是掌握其分页原理
    我做的时候用的是mysql数据库,如果你用的是mssql话就吧所有以mysql开头的换成sql就可以了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 MySql.Data.MySqlClient;
    using System.Data.SqlClient;
    using System.IO;
    public partial class mysql : System.Web.UI.Page
    {
        MySqlConnection conn = new MySqlConnection(System.Configuration.ConfigurationManager.AppSettings["constrmy"]);
        int ToatalCountRecord;//总记录数
        int PageItem = 4;//每页显示的条数
        int CurrentPage = 1;//当前页数
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!this.Page.IsPostBack)
            {
                if (Request.QueryString["page"] != null)
                {
                    if (!Int32.TryParse(Request.QueryString["page"].ToString(), out CurrentPage))
                    {
                        Response.Write("请输入分页参数!");
                        Response.End();
                        return;
                    }
                }
                
                this.BuidGrid();
            }
        }
        
        private void BuidGrid()
        {
            string s2 = "select * from ts1";
            MySqlDataAdapter da = new MySqlDataAdapter(s2, conn);
            DataSet ds = new DataSet();
            int startRecord = (CurrentPage - 1) * PageItem;
            da.Fill(ds, startRecord, PageItem, "a");
            this.DataList1.DataSource = ds.Tables["a"].DefaultView;
            this.DataList1.DataBind();
            MySqlCommand comm = new MySqlCommand("select count(*) from ts1", conn);
            conn.Open();
            ToatalCountRecord = Convert.ToInt32(comm.ExecuteScalar());
            conn.Close();
            BuildPages();
        }
        private void BuildPages()
        {
            int Step = 5;//偏移量
            int LeftNum = 0;//做界限
            int RightNum = 0;//右界限
            string PageUrl = Request.FilePath;
            int PageCount = (int)Math.Ceiling((double)(ToatalCountRecord) / PageItem);
            if (CurrentPage - Step < 1)
            {
                LeftNum = 1;
            }
            else
            {
                LeftNum = CurrentPage - Step;
            }
            if (CurrentPage + Step > PageCount)
            {
                RightNum = PageCount;
            }
            else
            {
                RightNum = CurrentPage + Step;
            }
            string OutPut = "";
            if (CurrentPage > 1)
            {
                OutPut += "<a href='" + PageUrl + "?page=" + (CurrentPage - 1) + "'>" + "上一页" + "</a>";
            }
            for (int i = LeftNum; i <= RightNum; i++)
            {
                if (i == CurrentPage)
                {
                    OutPut += "<font color=red>" + " " +"["+i.ToString() +"]"+ "" + "</font>";
                }
                else
                {
                    OutPut += "<a href='" + PageUrl + "?page=" + i.ToString() + "'>" + " " +"["+ i.ToString() +"]"+ " " + "</a>";
                }
            }
            if (CurrentPage < PageCount)
            {
                OutPut += "<a href='" + PageUrl + "?page=" + (CurrentPage + 1) + "'>" + "下一页" + "</a>";
            }
            this.PageInfo.InnerHtml = OutPut;
        }
      
    }
    需要在前台添加一个  <div id="PageInfo" runat="server" > 
      

  2.   

      /**//// <summary>
            /// 创建并显示分页器
            /// </summary>
            private void BuildPager(int totalRecords,int currentPage,int pageSize,int pid)
            {
                int alter = 4 ;
                int startPage = 1 ;
                int endPage = currentPage + alter ;
                int totalPages = this.CalculateTotalPages(totalRecords,pageSize) ;            if(currentPage > alter)
                {
                    startPage = currentPage - alter ;
                }            if(endPage > totalPages)
                {
                    endPage = totalPages ;
                }            string strTemp = @"<a href='PhotoList.aspx?pid={0}&pno={1}'>{2}</a>&nbsp;&nbsp;" ;
                StringBuilder sb = new StringBuilder("") ;
                if(currentPage != startPage)
                {
                    sb.Append( string.Format( strTemp , pid , 1 , "上一页" ) ) ;
                }            for( int i = startPage ; i <= endPage ; i++ )
                {
                    if( currentPage == i )
                    {
                        sb.Append("<font color=red>" + i + "</font>&nbsp;&nbsp;") ;
                    }
                    else
                    {
                        sb.Append( string.Format( strTemp , pid , i ,"[" + i + "]" ) ) ;
                    }
                }            if(currentPage != endPage)
                {
                    sb.Append( string.Format( strTemp , pid , currentPage + 1 , "下一页") ) ;
                }            this.ltlShowPager.Text = sb.ToString() ;
            }        /**//// <summary>
            /// 计算总页数
            /// </summary>
            /// <param name="totalRecords">总记录数</param>
            /// <param name="pageSize">每页记录数</param>
            private int CalculateTotalPages(int totalRecords, int pageSize) 
            {
                int totalPagesAvailable;            totalPagesAvailable = totalRecords / pageSize;            //由于C#的整形除法 会把所有余数舍入为0,所以需要判断是否需要加1
                if ((totalRecords % pageSize) > 0)
                    totalPagesAvailable++;            return totalPagesAvailable;
            }
      

  3.   

    static string constr = "server=.;user id=sa;password=;database=AntianxiaDB";
        SqlConnection conn = new SqlConnection(constr);
        int ToatalCountRecord;//总记录数
        int PageItem = 4;//每页显示的条数
        protected System.Web.UI.HtmlControls.HtmlGenericControl PageInfo;
        int CurrentPage = 1;//当前页数
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataGridBind();
            }    }
        public void DataGridBind()
        {
            string s2 = "select * from corp_users";
            SqlDataAdapter da = new SqlDataAdapter(s2, conn);
            DataSet ds = new DataSet();
            int startRecord = (CurrentPage - 1) * PageItem;
            da.Fill(ds, startRecord, PageItem, "a");
            this.DataList1.DataSource = ds.Tables["a"].DefaultView;
            this.DataList1.DataBind();
            SqlCommand comm = new SqlCommand("select count(*) from corp_users", conn);
            conn.Open();
            ToatalCountRecord = Convert.ToInt32(comm.ExecuteScalar());
            conn.Close();
            BuildPages();    }    private void BuildPages()
        {
            int Step = 5;//偏移量
            int LeftNum = 0;//做界限
            int RightNum = 0;//右界限
            string PageUrl = Request.FilePath;
            int PageCount = (int)Math.Ceiling((double)(ToatalCountRecord) / PageItem);
            if (CurrentPage - Step < 1)
            {
                LeftNum = 1;
            }
            else
            {
                LeftNum = CurrentPage - Step;
            }
            if (CurrentPage + Step > PageCount)
            {
                RightNum = PageCount;
            }
            else
            {
                RightNum = CurrentPage + Step;
            }
            string OutPut = "";
            if (CurrentPage > 1)
            {
                OutPut += "<a href='" + PageUrl + "?page=" + (CurrentPage - 1) + "'>" + "上一页" + "</a>";
            }
            for (int i = LeftNum; i <= RightNum; i++)
            {
                if (i == CurrentPage)
                {
                    OutPut += "<font color=red>" + " " + "[" + i.ToString() + "]" + "" + "</font>";
                }
                else
                {
                    OutPut += "<a href='" + PageUrl + "?page=" + i.ToString() + "'>" + " " + "[" + i.ToString() + "]" + " " + "</a>";
                }
            }
            if (CurrentPage < PageCount)
            {
                OutPut += "<a href='" + PageUrl + "?page=" + (CurrentPage + 1) + "'>" + "下一页" + "</a>";
            }
            this.pageInfo.InnerHtml = OutPut;
        }可是我点下一页都没反应!!!
      

  4.   

    你用的是2003还是2005?2003没反映啊要你就用 陕北吴旗娃 (webdiyer )的分页控件吧   很好用的,也很出名,也不用写那么多代码了了~http://www.webdiyer.com/AspNetPager/default.aspx
      

  5.   


    =================
    最有意义的也最值得LZ学习的。我想1楼的就是这个代码
      da.Fill(ds, startRecord, PageItem, "a");
    分页获取数据源方案
      

  6.   

    不可能
    我用了这么长时间了一点问题都没有的
    你好好看  你的什么地方的代码还有问题你加这个了吗
    需要在前台添加一个  <div id="PageInfo" runat="server" > 
      

  7.   

    http://blog.csdn.net/aaroncheng/archive/2008/12/03/3434893.aspx