使用DataList如何实现像GridView那种下面是一排数字的分页?

解决方案 »

  1.   

    前几天看到的
    http://topic.csdn.net/u/20081130/11/f52eebe9-e6f9-46de-b63a-f637b83aa944.html
      

  2.   

    datalist 没有自带分页,你可以写代码实现,但不如借用第三方快捷。aspnetpager不错的。
      

  3.   

     private int pageSize = 6;    private int i = 1;    protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ViewState["PageCount"] = 1;
                ViewState["pageIndex"] = 1;            BinerToDataList();
                IList<Topic> topics = GetAllProducts("select * from bbstopic");
                ViewState["PageCount"] = topics.Count / pageSize + 1;            i = topics.Count / pageSize+1;//new                     StringBuilder myt = new StringBuilder();
                while (i > 0)
                {
                    myt.Append("<a href=SectionManager.aspx?p=" + i + ">" + i + "</a>&nbsp;");
                    i--;
                }            mylink.Text = myt.ToString();
                      }               if (Request.QueryString["p"] != null)
            {
                PageIndex = Int32.Parse(Request.QueryString["p"]);
                BinerToDataList();
            }
               }    public int PageIndex
        {
            get { return (int)ViewState["pageIndex"]; }
            set
            {
                if ((int)value >= 1 && (int)value <= (int)ViewState["PageCount"])
                    ViewState["pageIndex"] = value;
            }
        }    public void BinerToDataList()
        {
            IList<Topic> topics = GetAllProducts("SELECT top " + pageSize + " *  FROM [bbstopic] where tid not in(select top " + (PageIndex - 1) * pageSize + " tid from [bbstopic])");
            //
            this.DataList1.DataSource = topics;
            this.DataList1.DataBind();
        }    public IList<Topic> GetAllProducts(string strsql)
        {
            IList<Topic> topics = new List<Topic>();
            using (SqlDataReader sdr = SqlHelper.GetReader(strsql))
            {
                while (sdr.Read())
                {
                    Topic top = new Topic();                top.TID = (int)sdr["TID"];
                    top.TlastReply = (DateTime)sdr["TlastReply"];
                    top.TreplyCount = (int)sdr["TreplyCount"];
                    top.Tstate = (int)sdr["tstate"];
                    top.Ttime = (DateTime)sdr["Ttime"];                topics.Add(top);
                }
            }
            return topics;
        }
    }记得在前台代码中增加一个HyperLink控件