分页查询
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { int Count = 5;//每页做多显示5条 List<car> query = new List<car>(); protected void Page_Load(object sender, EventArgs e) { Btn_Prev.Click += Btn_Prev_click;//上一页按钮 Btn_Next.Click += Btn_Next_Click;//下一页按钮 Btn_First.Click += Btn_First_Click;//首页 Btn_Last.Click += Btn_Last_Click;//末页 aaaa.SelectedIndexChanged += aaaa_SelectedIndexChanged; if (IsPostBack == false) { Repeater1.DataSource = new CarData().Select(Count, 1); Repeater1.DataBind(); txt_PageCount.Text = "1"; Btn_Prev.Enabled = false;//上一页失效 Btn_First.Enabled = false;//首页失效 //添加下拉列表数据 for (int i = 1; i <= MaxNumber(); i++) { ListItem li = new ListItem(i.ToString(), i.ToString()); aaaa.items.Add(li); } } } void aaaa_SelectedIndexChanged(object sender,EventArgs e) { //取出来要去往的页数 int yy=Convert.ToInt32(aaaa.SelectedItem.Value); query = new CarData().Select(Count ,yy);//cont 是每页有几条,YY是有几页 Repeater1.DataSource=query; Repeater1.DataBind(); txt_PageCount.Text=yy.ToString(); } //末页 void Btn_Last_Click(object sender,EventArgs e) { query =new CarData().Select(Count,MaxNumber()); Repeater1.DataSource=query; Repeater1.DataBind(); txt_PageCount.Text=MaxNumber().Tostring(); Btn_Next.Enabled=false; Btn_Last.Enabled=false; Btn_First.Enable=true; Btn_Prev.Enable=true; } //首页 void Btn_First_Click (object sender,EventArgs e) { query = new CarData().Select(count,1); Repeater1.DataSource=query; Repeater1.Databind(); txt_PageCount.text="1"; Btn_First.Enable=false; Btn_Prev.Enable=false; Btn_Last.Enable=true; Btn_Next.Enabled=true; } //下一页按钮 Void Btn_Next_Click (object sender,EventArges e) { //先看看是第几页,然后+1 int NowNum=Convert.ToInt32(txt_PageCount.Text)+1; query=new CarData().Select(Count,NowNum); Repeater1.DataSource=query; Repeater1.DataBind(); txt_PageCount.Text=NowNum.ToString(); //当前页数最大,下一页不可用 if(txt_PageCount.Text==MaxNumber().ToString ()) { Btn_Next.Enabled=false; Ben_Last.Enabled=false; } Btn_Prev.Enable=true;//上一页按钮变为可用 Btn_First.Enable=true;//首页可用 } //上一页按钮 void Btn_prev_Click(object sender,Event e) { //先看看是第几页,然后-1 int NowNumber=Convert.ToInt32(txt_PageCount.Text)-1; query = new CarData().Select(Count,NowNum); Repeater1.DataSource=query; Repeater1.DataBind(); txt_PageCount.Text=NowNum.ToString(); //只要按一下上一页按钮,就让下一页按钮变为可用 Btn_Next.Enable=true; Btn_Last.Enable=true; if (txt_PageCount.Text=="1") { Btn_Prev.Enabled=false; Btn_First.Enabled=false; } } public int MaxNumber() { List<car>List=new CarData().SelectAll();//全部数据的条数 double bb=list.Count/(Count*1.0);//全部条数初以每页显示的条数得到的就是最大的页数 return Convert.ToInt32(Math.Ceiling(bb));//将最大页数取上限,返回出去 } }