HTML
当前第【<asp:Label ID="Label_Now" runat="server" Text="Label"></asp:Label>】页 共【<asp:Label ID="Label_Max" runat="server" Text="Label"></asp:Label>】页 <asp:Button ID="btn_first" runat="server" Text="首页" /> <asp:Button ID="btn_prev" runat="server" Text="上一页" /> <asp:Button ID="btn_next" runat="server" Text="下一页" /> <asp:Button ID="btn_lest" runat="server" Text="末页" /> <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList> <asp:Button ID="Button1" runat="server" Text="跳转" />
public List<Warehouse> selects(int count,int NowPage) { List<Warehouse> war = new List<Warehouse>(); cmd.CommandText = "select top "+count+" * from Warehouse WHERE ids NOT IN (SELECT TOP "+(NowPage-1)*count+" ids FROM Warehouse)"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { Warehouse ware = new Warehouse(); ware.ids = Convert.ToInt32(dr[0].ToString()); ware.name = dr[1].ToString(); ware.Unit = Convert.ToDouble(dr[2].ToString()); ware.Number = Convert.ToInt32(dr[3].ToString()); ware.dtime = Convert.ToDateTime(dr[4].ToString()); ware.Shelf = Convert.ToInt32(dr[5].ToString()); ware.jname = dr[6].ToString(); ware.dianhua = dr[7].ToString(); war.Add(ware); } } conn.Close(); return war; }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class logo : System.Web.UI.Page { public string asc = null; int count = 5;//一页要显示的条数 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Label_Now.Text = "1"; Label_Max.Text = Max().ToString(); Repeater1.DataSource = new WarehouseDate().selects(count, 1); Repeater1.DataBind(); btn_prev.Enabled = false; int max = Max(); DropDownList1.Items.Clear(); for (int i = 1; i <= max; i++) { DropDownList1.Items.Add(new ListItem(i.ToString(), i.ToString())); } } #region asc = Request["ppp"]; Label1.Text = asc; if (Request.Cookies["name"] != null) { Label1.Text = Request.Cookies["name"].Value; } else Response.Redirect("Default.aspx"); Label1.Text += "登录成功,欢迎你!"; #endregion DropDownList1.TextChanged += DropDownList1_TextChanged; DropDownList1.AutoPostBack = true; btn_lest.Click += btn_lest_Click;//末页 btn_next.Click += btn_next_Click;//下一页 btn_first.Click += btn_first_Click;//首页 btn_prev.Click += btn_prev_Click;//上一页 Button1.Click += Button1_Click;//跳转 } void DropDownList1_TextChanged(object sender, EventArgs e) { //获取当前页,计算要看的下一页的页号; int nextpage = Convert.ToInt32(DropDownList1.SelectedValue); //判断是否有下一页 //if(nextpage>Max()) //{ // return; //} if (nextpage <= 1) { btn_prev.Enabled = false; } else { btn_prev.Enabled = true; } if (nextpage >= 12) { btn_next.Enabled = false; } else btn_next.Enabled = true; //按照获取的页数绑定相应的页数 Repeater1.DataSource = new WarehouseDate().selects(count, nextpage); Repeater1.DataBind(); //修改显示页数 Label_Now.Text = nextpage.ToString(); } void Button1_Click(object sender, EventArgs e) { //获取当前页,计算要看的下一页的页号; int nextpage = Convert.ToInt32(DropDownList1.SelectedValue); //判断是否有下一页 //if(nextpage>Max()) //{ // return; //} if (nextpage <= 1) { btn_prev.Enabled = false; } btn_next.Enabled = true; //按照获取的页数绑定相应的页数 Repeater1.DataSource = new WarehouseDate().selects(count, nextpage); Repeater1.DataBind(); //修改显示页数 Label_Now.Text = nextpage.ToString(); } void btn_prev_Click(object sender, EventArgs e) { //获取当前页,计算要看的下一页的页号; int nextpage = Convert.ToInt32(Label_Now.Text) - 1; //判断是否有下一页 //if(nextpage>Max()) //{ // return; //} if (nextpage <= 1) { btn_prev.Enabled = false; } btn_next.Enabled = true; //按照获取的页数绑定相应的页数 Repeater1.DataSource = new WarehouseDate().selects(count, nextpage); Repeater1.DataBind(); //修改显示页数 Label_Now.Text = nextpage.ToString(); DropDownList1.Text = nextpage.ToString(); } void btn_first_Click(object sender, EventArgs e) { //按照获取的页数绑定相应的页数 Repeater1.DataSource = new WarehouseDate().selects(count, 1); Repeater1.DataBind(); //修改显示页数 Label_Now.Text = "1"; btn_next.Enabled = true; btn_prev.Enabled = false; } void btn_next_Click(object sender, EventArgs e) { //获取当前页,计算要看的下一页的页号; int nextpage = Convert.ToInt32(Label_Now.Text) + 1; //判断是否有下一页 //if(nextpage>Max()) //{ // return; //} if (nextpage >= Max()) { btn_next.Enabled = false; } //按照获取的页数绑定相应的页数 Repeater1.DataSource = new WarehouseDate().selects(count, nextpage); Repeater1.DataBind(); btn_prev.Enabled = true; //修改显示页数 Label_Now.Text = nextpage.ToString(); DropDownList1.Text = nextpage.ToString(); } void btn_lest_Click(object sender, EventArgs e) { //按照获取的页数绑定相应的页数 Repeater1.DataSource = new WarehouseDate().selects(count, Max()); Repeater1.DataBind(); //修改显示页数 Label_Now.Text = Max().ToString(); btn_next.Enabled = false; btn_prev.Enabled = true; DropDownList1.Text = Max().ToString(); } /// <summary> /// 获取到最大的页数 /// </summary> /// <returns></returns> public int Max() { List<Warehouse> wlist = new WarehouseDate().select(); double End2 = Convert.ToInt32(Math.Ceiling(wlist.Count / (count * 1.0))); int End = Convert.ToInt32(End2); return End; } }
sql = "select * from Commodity"; hs = new Hashtable(); if (txt_name.Text.Trim() != "") { sql += " where Name like @Name"; hs.Add("@Name", "%" + txt_name.Text.Trim() + "%"); }
一、通过字符串操作,查看之前是否有Where if (txt_jgup.Text.Trim() != "") { if (sql.Contains("where")) sql += " and Price >= @Priceup"; else sql += " where Price >= @Priceup"; hs.Add("@Priceup", txt_jgup.Text.Trim()); }
二、通过标记变量
int count = 0; if (txt_numup.Text.Trim() != "") { if (count>0) sql += " and Stock >= @Stockup"; else sql += " where Stock >= @Stockup";
count++; hs.Add("@Stockup", txt_numup.Text.Trim()); }
三、拼接不参与搜索的用,1=1等替代
if (txt_name.Text.Trim() != "") { sql += " where Name like @Name"; hs.Add("@Name", "%" + txt_name.Text.Trim() + "%"); } else { sql += " where 1=1"; } if (txt_numup.Text.Trim() != "") { sql += " and Stock >= @Stockup"; hs.Add("@Stockup", txt_numup.Text.Trim()); }else { sql += " and 2=2"; }
组合后的分页查询语句需要添加,上面生成的语句,限定Select查询范围