datalist是不是没有pagesize属性啊????哪位朋友帖个完整点的分页代码,我刚刚开始学用datalist分页.跪求.....谢谢了.

解决方案 »

  1.   

    http://blog.csdn.net/21aspnet/archive/2004/10/25/150249.aspx
      

  2.   

    使用 aspnetpager 来实现  看看 http://www.webdiyer.com/AspNetPager/default.aspxdatalist repeater 都一样
      

  3.   


    //ASP.NET2.0中datalist仿百度分页
    //我本人做的时候用的是MYSQL数据库, 测试时请转化到MSSQL数据库即可
    //注意此方法在ASP.NET1.0或1.1中测试通不过,但是基本方法是没问题,关键是掌握其分页原理//要想用SQL数据库,把带有Mysql换成SQL就行,列如: MySqlConnection 换成SqlConnection
    //一下替换一样
    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" > 
      

  4.   

    对,找个版主写的aspnetpager,现在开源了,好用,利用dataset进行分页