一、分页查询
DataClassesDataContext data = new DataClassesDataContext(); int counts = 3; protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; Button2.Click += Button2_Click; Button3.Click += Button3_Click; Button4.Click += Button4_Click; Label2.Text = maxpage().ToString();//显示总页码if (!IsPostBack) { Label1.Text = "1";//设置当前页码显示 bind(counts, 0); Button1.Enabled = false; Button2.Enabled = false; if (Label1.Text == Label2.Text) { Button3.Enabled = false; Button4.Enabled = false; } } }/// <summary> /// 首页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void Button1_Click(object sender, EventArgs e) { bind(counts, 0);//绑定数据 Label1.Text = "1"; //首页中首页和上一页按钮不可用 Button1.Enabled = false; Button2.Enabled = false; Button3.Enabled = true; Button4.Enabled = true; } /// <summary> /// 上一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void Button2_Click(object sender, EventArgs e) { int page = Convert.ToInt32(Label1.Text) - 2;//获取页码 bind(counts, page); Label1.Text = (page + 1).ToString();//显示当前页 //到达第一页时首页和上一页按钮不可用 if (Label1.Text == "1") { Button1.Enabled = false; Button2.Enabled = false; } Button3.Enabled = true; Button4.Enabled = true; } /// <summary> /// 下一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void Button3_Click(object sender, EventArgs e) { int page = Convert.ToInt32(Label1.Text); bind(counts, page); Label1.Text = (page + 1).ToString(); Button1.Enabled = true; Button2.Enabled = true; //到达最后一页时末页和下一页按钮不可用 if (Label1.Text == Label2.Text) { Button3.Enabled = false; Button4.Enabled = false; } } /// <summary> /// 末页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void Button4_Click(object sender, EventArgs e) { bind(counts, maxpage() - 1);//绑定查询数据 Label1.Text = Label2.Text; //末页中下一页和末页按钮不可用 Button1.Enabled = true; Button2.Enabled = true; Button3.Enabled = false; Button4.Enabled = false; } /// <summary> /// 绑定数据 /// </summary> /// <param name="count"></param> /// <param name="page"></param> public void bind(int count, int page) { Repeater1.DataSource = (from message in data.goods select new { ID = message.ID, name = message.name, price = message.price, counts = message.counts }).Skip(counts * page).Take(counts); Repeater1.DataBind(); } /// <summary> /// 最大页 /// </summary> /// <returns></returns> public int maxpage() { int sum = data.goods.Count(); int page = sum / counts; int Page = sum % counts > 0 ? 1 : 0; int pages = page + Page; return pages; }
二、分页与组合查询
int count = 3; studentsDataContext stu = new studentsDataContext(); protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; Button2.Click += Button2_Click; Button3.Click += Button3_Click; Button4.Click += Button4_Click; Button5.Click += Button5_Click; if (!IsPostBack) { Label1.Text = "1"; Label2.Text = max().ToString(); bind(count, 0); } } /// <summary> /// 首页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void Button1_Click(object sender, EventArgs e) { if (TextBox1.Text.Trim().Length == 0 && TextBox2.Text.Trim().Length == 0 && TextBox3.Text.Trim().Length == 0 && TextBox4.Text.Trim().Length == 0 && TextBox5.Text.Trim().Length == 0) { Label1.Text = "1"; bind(count, 0); } else { List<score> lists = Data(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text); Label1.Text = "1"; Label2.Text = max(lists).ToString(); bind(lists, count, 0); } } /// <summary> /// 上一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void Button2_Click(object sender, EventArgs e) { if (TextBox1.Text.Trim().Length == 0 && TextBox2.Text.Trim().Length == 0 && TextBox3.Text.Trim().Length == 0 && TextBox4.Text.Trim().Length == 0 && TextBox5.Text.Trim().Length == 0) { int page = Convert.ToInt32(Label1.Text) - 2; Label1.Text = (page + 1).ToString(); bind(count, page); } else { List<score> lists = Data(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text); int page = Convert.ToInt32(Label1.Text) - 2; Label1.Text = (page + 1).ToString(); Label2.Text = max(lists).ToString(); bind(lists, count, 0); } } /// <summary> /// 下一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void Button3_Click(object sender, EventArgs e) { if (TextBox1.Text.Trim().Length == 0 && TextBox2.Text.Trim().Length == 0 && TextBox3.Text.Trim().Length == 0 && TextBox4.Text.Trim().Length == 0 && TextBox5.Text.Trim().Length == 0) { int page = Convert.ToInt32(Label1.Text); Label1.Text = (page + 1).ToString(); bind(count, page); } else { List<score> lists = Data(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text); int page = Convert.ToInt32(Label1.Text); Label1.Text = (page + 1).ToString(); Label2.Text = max(lists).ToString(); bind(lists, count, 0); } } /// <summary> /// 末页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void Button4_Click(object sender, EventArgs e) { if (TextBox1.Text.Trim().Length == 0 && TextBox2.Text.Trim().Length == 0 && TextBox3.Text.Trim().Length == 0 && TextBox4.Text.Trim().Length == 0 && TextBox5.Text.Trim().Length == 0) { int page = max() - 1; Label1.Text = Label2.Text; bind(count, page); } else { List<score> lists = Data(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text); int page = max(lists); Label1.Text = (page + 1).ToString(); Label2.Text = max(lists).ToString(); bind(lists, count, 0); } } /// <summary> /// 查询 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void Button5_Click(object sender, EventArgs e) { List<score> lists = Data(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text); Label1.Text = "1"; Label2.Text = max(lists).ToString(); bind(lists, count, 0); } /// <summary> /// 没有查询条件时的数据绑定 /// </summary> /// <param name="count"></param> /// <param name="page"></param> public void bind(int count, int page) { Repeater1.DataSource = stu.score.Skip(count * page).Take(count); Repeater1.DataBind(); Button1.Enabled = true; Button2.Enabled = true; Button3.Enabled = true; Button4.Enabled = true; if (Label1.Text == "1") { Button1.Enabled = false; Button2.Enabled = false; } if (Label1.Text == max().ToString()) { Button3.Enabled = false; Button4.Enabled = false; } } /// <summary> /// 有查询条件时的数据绑定 /// </summary> /// <param name="List"></param> /// <param name="count"></param> /// <param name="page"></param> public void bind(List<score> List, int count, int page) { Repeater1.DataSource = List.Skip(count * page).Take(count); Repeater1.DataBind(); Button1.Enabled = true; Button2.Enabled = true; Button3.Enabled = true; Button4.Enabled = true; if (Label1.Text == "1") { Button1.Enabled = false; Button2.Enabled = false; } if (Label1.Text == max(List).ToString()) { Button3.Enabled = false; Button4.Enabled = false; } } /// <summary> /// 没有查询条件时的最大页 /// </summary> /// <returns></returns> public int max() { int sum = stu.score.Count(); int page = sum / count; int pages = sum % count > 0 ? 1 : 0; int Page = page + pages; return Page; } /// <summary> /// 有查询条件的最大页 /// </summary> /// <param name="List"></param> /// <returns></returns> public int max(List<score> List) { int sum = List.Count(); int page = sum / count; int pages = sum % count > 0 ? 1 : 0; int Page = page + pages; return Page; } /// <summary> /// 组合查询方法 /// </summary> /// <param name="text1"></param> /// <param name="text2"></param> /// <param name="text3"></param> /// <param name="text4"></param> /// <param name="text5"></param> /// <returns></returns> public List<score> Data(string text1, string text2, string text3, string text4, string text5) { var list1 = stu.score.AsQueryable(); var list2 = stu.score.AsQueryable(); var list3 = stu.score.AsQueryable(); var list4 = stu.score.AsQueryable(); var list5 = stu.score.AsQueryable(); if (TextBox1.Text.Trim().Length > 0) { list1 = list1.Where(s => s.student.name.Contains(text1)); } if (TextBox2.Text.Trim().Length > 0) { list2 = list2.Where(s => s.student.nation1.naname.Contains(text2)); } if (TextBox3.Text.Trim().Length > 0) { list3 = list3.Where(s => Convert.ToDateTime(s.student.birth).Year == Convert.ToInt32(text3)); } if (TextBox4.Text.Trim().Length > 0) { list4 = list4.Where(s => s.subject.subname == text4); } if (TextBox5.Text.Trim().Length > 0) { if (DropDownList1.Text == ">") { list5 = list5.Where(s => s.score1 > Convert.ToDecimal(text5)); } else if (DropDownList1.Text == "=") { list5 = list5.Where(s => s.score1 == Convert.ToDecimal(text5)); } else { list5 = list5.Where(s => s.score1 < Convert.ToDecimal(text5)); } } var list = list1.Intersect(list2).Intersect(list3).Intersect(list4).Intersect(list5); List<score> lists = list.ToList(); return lists; }