【IT168技术文档】
与PageDataSource相结合实现其分页功能。
using System; using System.Data; using System.Configuration; 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; public partial class _Default : System.Web.UI.Page { DataTable table = new DataTable(); PagedDataSource PDS=new PagedDataSource(); protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { InitData(); } } //初始化数据,把数据添加到Session中,避免重复查询数据库。 void InitData() { SqlConnection conn = new SqlConnection("Data Source=Localhost;Initial Catalog=Northwind;User ID=sa"); SqlCommand cmm = new SqlCommand("SELECT [CustomerID], [CompanyName] FROM [Customers]"); cmm.Connection = conn; SqlDataAdapter DA = new SqlDataAdapter(); DA.SelectCommand = cmm; DataSet DS = new DataSet(); DA.Fill(DS); table = DS.Tables[0]; if (Session["data"] != null) { Session["data"] =table; } else { Session.Add("data",table); } Pagging(0); } //实现分页功能 protected void Pagging(int index) { PDS.DataSource =((DataTable)Session["data"]).DefaultView; PDS.CurrentPageIndex = index; PDS.AllowPaging = true; PDS.PageSize = 20; this.Repeater2.DataSource = PDS; this.Repeater2.DataBind(); DropDownList ddl = (DropDownList)this.Repeater2.Controls[this.Repeater2.Controls.Count - 1].FindControl("PageCount"); if (ddl != null) { for (int i = 1; i < PDS.PageCount; i++) { ddl.Items.Add(i.ToString()); } } } public void PageIndex(object sender, EventArgs e) { DropDownList ddl = (DropDownList)this.Repeater2.Controls[this.Repeater2.Controls.Count - 1].FindControl("PageCount"); Pagging(int.Parse(ddl.SelectedItem.Text)-1); } }