前台aspx:
public partial class _Default : System.Web.UI.Page{ int count = 5; int page = 1; int max = 0; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { using (DatamydbDataContext con = new DatamydbDataContext()) { Repeater1.DataSource = con.Car.Take(count); Repeater1.DataBind(); Label1.Text = Convert.ToInt32(Math.Ceiling(con.Car.Count() * 1.0 / count)).ToString(); } } Button_prev.Click += Button_prev_Click; Button_next.Click += Button_next_Click; Button1.Click += Button1_Click; Button_first.Click += Button_first_Click; Button_last.Click += Button_last_Click; } void Button_last_Click(object sender, EventArgs e) { page = Convert.ToInt32(Label1.Text); select(); } void Button_first_Click(object sender, EventArgs e) { page = 1; select(); } void Button1_Click(object sender, EventArgs e) { page = 1; select(); } void Button_next_Click(object sender, EventArgs e) { page = Convert.ToInt32(Label1_now.Text) + 1; select(); } void Button_prev_Click(object sender, EventArgs e) { page = Convert.ToInt32(Label1_now.Text) - 1; select(); } public void select() { using (DatamydbDataContext con = new DatamydbDataContext()) { IQueryableclist = con.Car.AsQueryable(); if (TextBox1.Text.Trim().Length > 0) { clist = clist.Where(r=>r.Name.Contains(TextBox1.Text)); } if (TextBox2.Text.Trim().Length > 0) { clist = clist.Where(r=>r.Brand.Contains(TextBox2.Text)); } if (TextBox3.Text.Trim().Length > 0) { decimal price = Convert.ToDecimal(TextBox3.Text); string st = DropDownList1.SelectedValue; if (st == "=") { clist = clist.Where(r=>r.Price==price); } if (st == ">") { clist = clist.Where(r => r.Price > price); } if (st == "<") { clist = clist.Where(r => r.Price < price); } } if (page < 1) { page++; return; } if (page > Convert.ToInt32(Math.Ceiling(clist.Count() * 1.0 / count))) { page--; return; } Repeater1.DataSource = clist.Skip((page - 1) * count).Take(count); Repeater1.DataBind(); Label1_now.Text = page.ToString(); max = Convert.ToInt32(Math.Ceiling(clist.Count() * 1.0 / count)); Label1.Text = max.ToString(); } } }