查询里的方法
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// CarBF 的摘要说明 /// </summary> public class CarBF { private DataClassesDataContext _Context; public CarBF() { _Context = new DataClassesDataContext(); } public List<Car> Select() { return _Context.Car.ToList(); } public List<ChinaStates> Select1() { return _Context.ChinaStates.ToList(); } //Take查询前五条 public List<ChinaStates> Select2() { return _Context.ChinaStates.Take(5).ToList(); } //nowpage是当前页,numbers是一页为几条数据.skip是跳过序列中指定数量的元素 public List<ChinaStates> Select3(int nowpage,int numbers) { return _Context.ChinaStates.Skip((nowpage - 1) * numbers).Take(numbers).ToList(); } }
aspx.cs里的代码
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 { protected void Page_Load(object sender, EventArgs e) { //List<Car> list = new CarBF().Select(); //Cache.Insert("aaa",list); if (!IsPostBack) { bangding(1, 5); } } //点击首页 protected void LinkButton1_Click(object sender, EventArgs e) { bangding(1,5); } public void bangding(int nowpage,int numbers) { List<ChinaStates> list = new CarBF().Select3(nowpage, numbers); Repeater1.DataSource = list; Repeater1.DataBind(); TextBox1.Text = nowpage.ToString(); } //点击尾页 protected void LinkButton2_Click(object sender, EventArgs e) { List<ChinaStates> list = new CarBF().Select1(); if (list.Count%5==0) { bangding(list.Count/5,5); } else if (list.Count%5!=0) { bangding(list.Count/5+1,5); } } //点击上一页 protected void LinkButton3_Click(object sender, EventArgs e) { int nowpage = Convert.ToInt32(TextBox1.Text); if (Convert.ToInt32(TextBox1.Text) > 1) { bangding(nowpage - 1, 5); } else { LinkButton3.Enabled = false; } } //点击下一页 protected void LinkButton4_Click(object sender, EventArgs e) { List<ChinaStates> list = new CarBF().Select1(); int count = list.Count / 5; if (Convert.ToInt32(TextBox1.Text) <= count) { int nowpage = Convert.ToInt32(TextBox1.Text); bangding(nowpage + 1, 5); } else { LinkButton4.Enabled = false; } } //当点击转到页的时候 protected void LinkButton5_Click(object sender, EventArgs e) { List<ChinaStates> list = new CarBF().Select1(); if (Convert.ToInt32(TextBox1.Text) > list.Count / 5 + 1) { TextBox1.Text = null; Literal1.Text = "页数超出范围"; } else { int count = Convert.ToInt32(TextBox1.Text); bangding(count, 5); } } }
效果图
时间: 2024-10-07 12:26:26