首页 > 代码库 > 分页查询、组合查询
分页查询、组合查询
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class fenye : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Repeater1.DataSource = new ChainDA().Select(Pagcount, 1);//默认是5条数据,第一页 Repeater1.DataBind(); Label1.Text = "1";//打开就显示当前第1页 for (int i = 1; i <= Max(); i++) { //每一条数据都是ListItem ListItem li = new ListItem(i.ToString(),i.ToString()); //复合控件 DropDownList1.Items.Add(li); } } Label2.Text = Max().ToString();//显示总共多少页 btn_first.Click += btn_first_Click;//首页按钮 btn_end.Click += btn_end_Click;//末页按钮 btn_prev.Click += btn_prev_Click;//上一页按钮 btn_next.Click += btn_next_Click;//下一页按钮 Button1.Click += Button1_Click;//点击跳转按钮 DropDownList1.SelectedIndexChanged += Button1_Click; } void Button1_Click(object sender, EventArgs e) {//跳转按钮 int a = Convert.ToInt32(DropDownList1.SelectedItem.Text); Label1.Text = a.ToString(); Repeater1.DataSource = new ChainDA().Select(Pagcount, a); Repeater1.DataBind();//重新赋值 } int Pagcount = 10;//每页显示多少条 //下一页按钮 void btn_next_Click(object sender, EventArgs e) { int num = Convert.ToInt32(Label1.Text);// num++;//下一页 if (num >= Max())//超过最大页就不执行了,下面语句也不执行 { return; } Label1.Text = num.ToString(); Repeater1.DataSource = new ChainDA().Select(Pagcount, num);//显示Pagcount条数据,num是下一页 Repeater1.DataBind();//重新赋值 } //上一页按钮 void btn_prev_Click(object sender, EventArgs e) { int num = Convert.ToInt32(Label1.Text);// num--;//上一页 if (num < 1)//超过最小页就不执行了,下面语句也不执行 { return; } Label1.Text = num.ToString(); Repeater1.DataSource = new ChainDA().Select(Pagcount, num);//显示Pagcount条数据,num是上一页 Repeater1.DataBind();//重新赋值 } //末页按钮 void btn_end_Click(object sender, EventArgs e) { Repeater1.DataSource = new ChainDA().Select(Pagcount, Max());//显示Pagcount条数据,Max()是最大页 Repeater1.DataBind();//重新赋值 Label1.Text = Max().ToString(); } //首页按钮 void btn_first_Click(object sender, EventArgs e) { Repeater1.DataSource = new ChainDA().Select(Pagcount, 1);//显示Pagcount条数据,1是首页 Repeater1.DataBind();//重新赋值 Label1.Text ="1"; } private int Max()//最大页 { int count = new ChainDA().Select().Count; double aa= count/(Pagcount*1.0);//所有的数据除以每页显示的条数就是一共多少页 return Convert.ToInt32(Math.Ceiling(aa));//返回最大或等于的整数 }}
组合查询:
View Code
using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class Default2 : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Repeater1.DataSource = new CarData().Select("select *from Car", new Hashtable()); Repeater1.DataBind(); } Button2.Click += Button2_Click; } void Button2_Click(object sender, EventArgs e) { Hashtable hs = new Hashtable();//哈希表集合,自定义 int count = 0;//中间变量 string Tsql = "select *from car";//默认查全部 if (TextBox1.Text != "") { Tsql += " where name like @a";//防攻击 hs.Add("@a", "%" + TextBox1.Text + "%"); count++; } if (TextBox2.Text != "") { if (count > 0) { Tsql += " and oil=@b"; } else { Tsql += " where oil=@b"; } count++; hs.Add("@b", TextBox2.Text); } if (TextBox3.Text != "") { if (count > 0) { Tsql += " and powers = @c"; } else { Tsql += " where powers = @c"; } hs.Add("@c", TextBox3.Text); } Label3.Text = Tsql; Repeater1.DataSource = new CarData().Select(Tsql, hs); Repeater1.DataBind(); // select *from car where name like "%"+ + "%" "and oil=" + + "and powers=" + +; }}
分页查询、组合查询
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。