回 帖 发 新 帖 刷新版面

主题:repeater分页

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.Data.SqlClient;


namespace dataBind
{
    public partial class repeaterControl : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.Label3.Text ="1";
                this.dataBindToRepeater();
            }
        }
        private void dataBindToRepeater()
        {
            int curPage = Convert.ToInt32(this.Label3.Text);
            SqlConnection con = DBcs.createCon();
            SqlCommand cmd = new SqlCommand("select * from Employees", con);
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            DataSet ds = new DataSet();
            sda.Fill(ds, "emp");
            PagedDataSource ps = new PagedDataSource();
            ps.DataSource = ds.Tables["emp"].DefaultView;
            ps.AllowPaging = true;
            ps.PageSize = 3;
            ps.CurrentPageIndex = curPage - 1;
            this.Button1.Enabled = true;
            this.Button2.Enabled = true;
            if (curPage == 1)
            {
                this.Button1.Enabled = false;
            }
            if (curPage == ps.PageCount)
            {
                this.Button2.Enabled = false;
            }
            this.Repeater1.DataSource = ds.Tables["emp"];
            this.Repeater1.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            this.Label3.Text = Convert.ToString(Convert.ToInt32(this.Label3.Text) - 1);
            this.dataBindToRepeater();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            this.Label3.Text = Convert.ToString(Convert.ToInt32(this.Label3.Text) + 1);
            this.dataBindToRepeater();
        }
    }
}


为什么这段代码不能实现repeater分页?

回复列表 (共1个回复)

沙发

原因很简单,你使用了PagedDataSource对象来给数据源分页,但你在绑定数据的时候使用的数据源是dataset而不是这个PagedDataSource,那么你这个分页部分就没有用上所以不起作用.
改成:this.Repeater1.DataSource = ps;
this.Repeater1.DataBind();

我来回复

您尚未登录,请登录后再回复。点此登录或注册