repeater和datalist控件可以很快的、灵活地在.aspx页面上显示数据,
但它们都没有分页功能;虽然datagrid控件有分页功能,但使用起来却太古板了、灵活性太差了。
用pagedatasource这个类可以在repeater中进行分页:
pagedatasource是datagrid中封装的一个类,datagrid就是用这个来实现分页功能的;
也可以用这个类在datalist、repeater中进行分页
public void Page_Load(Object src, EventArgs e) { SqlConnection cnn = new SqlConnection( " xxx " ); // 连接数据库 SqlDataAdapter mycommand = new SqlDataAdapter( " select * from start1 " ,cnn); // 数据操作,而表start1及其数据自己sqlserver中做 DataSet ds = new DataSet(); mycommand.Fill(ds); // 实例dataset对象为ds,并把数据填充到ds上 PagedDataSource pp = new PagedDataSource(); // 对分页功能的类实例对象 pp.DataSource = ds.Tables[ 0 ].DefaultView; // 把数据赋予对象pp pp.AllowPaging = true ; // 允许进行分页 pp.PageSize = 6 ; // 设置每页数据的个数 int cpage; // 这个整数用来分析分页页数的 if (Request.QueryString[ " page " ] != null ) // 这个判断语句的作用是对cpage进行赋值 cpage = Convert.ToInt32(Request.QueryString[ " page " ]); else cpage = 1 ; pp.CurrentPageIndex = cpage - 1 ; // pp对象的当前引索值,因为引索值是从0开始,cpage从1开始所以要减1 if ( ! pp.IsFirstPage) // Request.CurrentExecutionFilePath为当前的程序的文件名,直接写也可以 pre.NavigateUrl = Request.CurrentExecutionFilePath + " ?page= " + Convert.ToString(cpage - 1 ); if ( ! pp.IsLastPage) next.NavigateUrl = Request.CurrentExecutionFilePath + " ?page= " + Convert.ToString(cpage + 1 ); repeater1.DataSource = pp; repeater1.DataBind(); }