标签:cte end button items read 条件查询 protect selectall number
实体类
public int ids { get; set; } public string code { get; set; } public string name { get; set; } public decimal oil { get; set; } public decimal price { get; set; }
封装类
注意引用using System.Data.SqlClient;
SqlConnection conn = null; SqlCommand cmd = null; public carData() { conn = new SqlConnection("server=.;database=One;user=sa;pwd=123"); cmd = conn.CreateCommand(); } //查询全部 public List<car> SelectAll() { List<car> clist = new List<car>(); cmd.CommandText = "select * from car"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { car c = new car(); c.ids = Convert.ToInt32(dr["ids"]); c.code = dr["code"].ToString(); c.name = dr["name"].ToString(); c.oil = Convert.ToDecimal(dr["oil"]); c.price = Convert.ToDecimal(dr["price"]); clist.Add(c); } } conn.Close(); return clist; } //分页查询 public List<car> SelectAll(int count, int number) { List<car> clist = new List<car>(); cmd.CommandText = "select top " + count + " * from car where ids not in(select top " + (count * (number - 1)) + " ids from car)"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { car c = new car(); c.ids = Convert.ToInt32(dr["ids"]); c.code = dr["code"].ToString(); c.name = dr["name"].ToString(); c.oil = Convert.ToDecimal(dr["oil"]); c.price = Convert.ToDecimal(dr["price"]); clist.Add(c); } } conn.Close(); return clist; } //查询数据条数 public int SelectCount() { int a = 0; cmd.CommandText = "select count(*) from car"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); dr.Read(); a = Convert.ToInt32(dr[0]); conn.Close(); return a; } //条件查询 public List<car> SelectAll(string tsql, Hashtable hs) { List<car> clist = new List<car>(); cmd.CommandText = tsql; cmd.Parameters.Clear(); foreach(string h in hs.Keys) { cmd.Parameters.AddWithValue(h,hs[h]); } conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { car c = new car(); c.ids = Convert.ToInt32(dr["ids"]); c.code = dr["code"].ToString(); c.name = dr["name"].ToString(); c.oil = Convert.ToDecimal(dr["oil"]); c.price = Convert.ToDecimal(dr["price"]); clist.Add(c); } } conn.Close(); return clist; }
前端
<table style="width: 100%; background-color: #0094ff; text-align: center;"> <tr> <td>Ids</td> <td>编号</td> <td>名称</td> <td>油耗</td> <td>价格</td> </tr> <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <tr style="background-color: #fff;"> <td><%#Eval("ids") %></td> <td><%#Eval("code") %></td> <td><%#Eval("name") %></td> <td><%#Eval("oil") %></td> <td><%#Eval("price") %></td> </tr> </ItemTemplate> </asp:Repeater> </table> <br /> 当前第【<asp:Label ID="Label1" runat="server" Text="1"></asp:Label>】页 共【<asp:Label ID="Label2" runat="server" Text="1"></asp:Label>】页 <asp:Button ID="Button1" runat="server" Text="首页" /> <asp:Button ID="Button2" runat="server" Text="上一页" /> <asp:Button ID="Button3" runat="server" Text="下一页" /> <asp:Button ID="Button4" runat="server" Text="尾页" /> <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList><asp:Button ID="Button5" runat="server" Text="跳转" />
后台
int count = 5;//每页显示条数 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Repeater1.DataSource = new carData().SelectAll(count, 1); Repeater1.DataBind(); Label2.Text = MaxPageNumber().ToString(); for (int i = 1; i <= MaxPageNumber(); i++) { ListItem li = new ListItem(i.ToString(),i.ToString()); DropDownList1.Items.Add(li); } } //改变按钮是否可用状态 if (Label1.Text == "1") { Button1.Enabled = false; Button2.Enabled = false; } if (Label1.Text != "1") { Button1.Enabled = true; Button2.Enabled = true; } if (Label1.Text == MaxPageNumber().ToString()) { Button3.Enabled = false; Button4.Enabled = false; } if (Label1.Text != MaxPageNumber().ToString()) { Button3.Enabled = true; Button4.Enabled = true; } //按钮点击事件 Button4.Click += Button4_Click; Button3.Click += Button3_Click; Button2.Click += Button2_Click; Button1.Click += Button1_Click; Button5.Click += Button5_Click; } //跳转 void Button5_Click(object sender, EventArgs e) { int a = Convert.ToInt32(DropDownList1.SelectedValue); //将下一页数据绑定到 Repeater1.DataSource = new carData().SelectAll(count, a); Repeater1.DataBind(); //将当前显示的页数改变到页面上去 Label1.Text = a.ToString(); //改变按钮是否可用状态 if (Label1.Text == "1") { Button1.Enabled = false; Button2.Enabled = false; } if (Label1.Text != "1") { Button1.Enabled = true; Button2.Enabled = true; } if (Label1.Text == MaxPageNumber().ToString()) { Button3.Enabled = false; Button4.Enabled = false; } if (Label1.Text != MaxPageNumber().ToString()) { Button3.Enabled = true; Button4.Enabled = true; } } //首页 void Button1_Click(object sender, EventArgs e) { //将下一页数据绑定 Repeater1.DataSource = new carData().SelectAll(count, 1); Repeater1.DataBind(); //将当前显示的页数改变 Label1.Text = "1"; //改变按钮是否可用状态 if (Label1.Text == "1") { Button1.Enabled = false; Button2.Enabled = false; } else { Button1.Enabled = true; Button2.Enabled = true; } } //上一页 void Button2_Click(object sender, EventArgs e) { //获取当前页数,计算上一页页数 int NextNumber = Convert.ToInt32(Label1.Text) - 1; if (NextNumber < 1) { return; } //将上一页数据绑定 Repeater1.DataSource = new carData().SelectAll(count, NextNumber); Repeater1.DataBind(); //将当前显示的页数改变 Label1.Text = NextNumber.ToString(); //改变按钮是否可用状态 if (Label1.Text == "1") { Button1.Enabled = false; Button2.Enabled = false; } else { Button1.Enabled = true; Button2.Enabled = true; } if (Label1.Text == MaxPageNumber().ToString()) { Button3.Enabled = false; Button4.Enabled = false; } else { Button3.Enabled = true; Button4.Enabled = true; } } //下一页 void Button3_Click(object sender, EventArgs e) { //获取当前页数,计算下一页页数 int NextNumber = Convert.ToInt32(Label1.Text) + 1; if (NextNumber > MaxPageNumber()) { return; } //将下一页数据绑定 Repeater1.DataSource = new carData().SelectAll(count, NextNumber); Repeater1.DataBind(); //将当前显示的页数改变 Label1.Text = NextNumber.ToString(); //改变按钮是否可用状态 if (Label1.Text == MaxPageNumber().ToString()) { Button3.Enabled = false; Button4.Enabled = false; } else { Button3.Enabled = true; Button4.Enabled = true; } if (Label1.Text == "1") { Button1.Enabled = false; Button2.Enabled = false; } else { Button1.Enabled = true; Button2.Enabled = true; } } //尾页 void Button4_Click(object sender, EventArgs e) { //将下一页数据绑定 Repeater1.DataSource = new carData().SelectAll(count, MaxPageNumber()); Repeater1.DataBind(); //将当前显示的页数改变 Label1.Text = MaxPageNumber().ToString(); //改变按钮是否可用状态 if (Label1.Text == MaxPageNumber().ToString()) { Button3.Enabled = false; Button4.Enabled = false; } else { Button3.Enabled = true; Button4.Enabled = true; } } //取最大页数 public int MaxPageNumber() { int a = 0; int maxcount = new carData().SelectCount(); decimal d = Convert.ToDecimal(maxcount) / count; a = Convert.ToInt32(Math.Ceiling(d)); return a; }
标签:cte end button items read 条件查询 protect selectall number
原文地址:http://www.cnblogs.com/hanqi0216/p/6902403.html