我用DataList分页,我现在只会 首页 上一页 下一页 尾页 这种分页,请问如何实现这样的分页
 首页 上一页 12345...下一页 尾页 ,主要是中间的12345...怎么实现

解决方案 »

  1.   

    用aspnetpager可以实现.但是否控制不了样式.
      

  2.   

    pager?是什么控件,我怎么没用过
      

  3.   

      参考吴旗娃的分页控件,或 PageDataSource   用法
      

  4.   

    asp.net分页控件多好用啊,样式应该可以把,不过我没有试过。或者用C#自己写一个分页程序,那么样式可以自己做主了
      

  5.   

    http://www.cnblogs.com/itelite/archive/2007/10/24/935483.html
      

  6.   

    随便写了个呵呵 抛砖引玉
                    PagedDataSource ps = new PagedDataSource();
                    ps.DataSource = dt.DefaultView;
                    ps.AllowPaging = true;
                    ps.PageSize = 10;
                    int pagecount;
                    if (Request.QueryString["page"] != null)
                        pagecount = Convert.ToInt32(Request.QueryString["page"].ToString());
                    else
                        pagecount = 1;                ps.CurrentPageIndex = pagecount - 1;                                
                    lblPagecount.Text = ps.PageCount.ToString();                
                    
                    lblList.Text = "";
                    
                    int MinPage = pagecount - 5;
                    int MaxPage = pagecount + 5;
                    if(MaxPage>ps.PageCount)
                        MaxPage=ps.PageCount;
                    if (MinPage < 0)
                        MinPage = 0;               
                    for (int i = MinPage; i < MaxPage; i++)
                    {                        string url = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(i+1);                        string pg = Convert.ToString(i + 1);
                            if (i == pagecount-1)
                                pg = "<Font color=red>" + pg + "</font>";
                            lblList.Text += "<a href=" + url + " >[ " + pg + " ]</a>  ";                }                lblList.Text += "...";                    //Response.Write(ts.Rows[0][0]);
                        if (!ps.IsFirstPage)
                            this.hplFist.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1);                if (!ps.IsFirstPage)
                        this.hplPrv.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(pagecount - 1);                if (!ps.IsLastPage)
                        this.hplNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(pagecount + 1);                if (!ps.IsLastPage)
                        this.hplLast.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(ps.PageCount);
                    rep_Userans.DataSource = ps;
                    rep_Userans.DataBind();
      

  7.   


    //ASP.NET2.0中datalist仿百度分页 
    //注意这个分页在1.0中无效 
    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 

        SqlConnection conn = new SqlConnection(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"; 
            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 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" >
      

  8.   

    你直接用for循环把页码输出即可,最多就加上一些限制显示的页码数的判断,比如不能让上一页的页码小于1,也不能让下一页的页码大于总页数等