组合查询 分页
Posted 涤荡轮回
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了组合查询 分页相关的知识,希望对你有一定的参考价值。
分页:
当前第<asp:Label ID="Label1" runat="server" Text=""></asp:Label>页 共<asp:Label ID="Label2" runat="server" Text=""></asp:Label>页 <asp:LinkButton ID="LinkButton1" runat="server" >首页</asp:LinkButton> <asp:LinkButton ID="LinkButton2" runat="server">上一页</asp:LinkButton> <asp:LinkButton ID="LinkButton3" runat="server">下一页</asp:LinkButton> <asp:LinkButton ID="LinkButton4" runat="server">末页</asp:LinkButton> 跳到 <asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server"></asp:DropDownList> 页
public List<Users> cha(int page,int pagecode) { List<Users> List = new List<Users>(); Users u = null;
//注意:sql语句 com.CommandText = "select top " + pagecode + " *FROM Users where UserName not in (select top " + ((page - 1) * pagecode) + " UserName FROM Users)"; con.Open(); SqlDataReader dr = com.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { u = new Users() { UserName = dr[0].ToString(), PassWord = dr[1].ToString(), NickName = dr[2].ToString(), Sex = Convert.ToBoolean(dr[3]), Birthday = Convert.ToDateTime(dr[4]), Phone = dr[5].ToString(), Email = dr[6].ToString(), Natoin = dr[7].ToString(), Area = dr[8].ToString(), }; List.Add(u); } } con.Close(); return List; }
全局变量,每页显示的个数
int pagecode = 5; //跳到指定页数 void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { int a=Convert.ToInt32(DropDownList1.SelectedValue);//获取页数
//显示数据 Repeater1.DataSource = new Usersdata().cha(a, pagecode); Repeater1.DataBind(); Label1.Text = a.ToString();//显示当前页 } //下一页 void LinkButton3_Click(object sender, EventArgs e) { Label1.Text = (Convert.ToInt32(Label1.Text) + 1).ToString();//显示下一页 if (Convert.ToInt32(Label1.Text) <= MAXPAGE(pagecode))//判断是否小于最大页 { Repeater1.DataSource = new Usersdata().cha(Convert.ToInt32(Label1.Text), pagecode); Repeater1.DataBind(); } else { Label1.Text = MAXPAGE(pagecode).ToString(); return; } } //上一页 void LinkButton2_Click(object sender, EventArgs e) { Label1.Text =(Convert.ToInt32(Label1.Text) - 1).ToString();//显示上一页 if (Convert.ToInt32(Label1.Text)<=0)//判断是否大于0页 { Label1.Text = "1"; return; } Repeater1.DataSource = new Usersdata().cha(Convert.ToInt32(Label1.Text), pagecode); Repeater1.DataBind(); } //末页 void LinkButton4_Click(object sender, EventArgs e) { Repeater1.DataSource = new Usersdata().cha(MAXPAGE(pagecode), pagecode); Repeater1.DataBind(); Label1.Text = MAXPAGE(pagecode).ToString(); } //首页 void LinkButton1_Click(object sender, EventArgs e) { Repeater1.DataSource = new Usersdata().cha(1, pagecode); Repeater1.DataBind(); Label1.Text = "1"; } //最大页数 private static int MAXPAGE(int pagecode) { double ye = (new Usersdata().cha().Count) / (pagecode *1.0); int maxye = Convert.ToInt32(Math.Ceiling(ye)); return maxye; }
//查找按钮事件
Hashtable has = new Hashtable();//实例化哈希表,作用防攻击
//拼sql语句 string sqls = "select *from Users "; if(TextBox1.Text!="") { sqls += " where UserName like @a"; has.Add("@a", "%" + TextBox1.Text.Trim() + "%");//将用户输入的内容放进哈希表,防攻击 count++; } if(Drop_nation.SelectedValue !="0") { if(count !=0)//如果上面有查询条件接and { sqls += " and [email protected]"; } else//如果上面没有查询条件接where { sqls += " WHERE [email protected]"; count++; } string sss=Drop_nation.SelectedValue.ToString(); has.Add("@b",sss ); count++; }
//Repeater数据绑定
Repeater1.DataSource = new Usersdata().chas(sqls,has);
Repeater1.DataBind();
count = 0;
public List<Users> chas(string s,Hashtable has) //语句 哈希表
{
List<Users> List = new List<Users>();
Users u = null;
com.CommandText = s;
com.Parameters.Clear();
foreach(string aa in has.Keys)
{
com.Parameters.Add(aa,has[aa]);
}
con.Open();
SqlDataReader dr = com.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
u = new Users()
{
UserName = dr[0].ToString(),
PassWord = dr[1].ToString(),
NickName = dr[2].ToString(),
Sex = Convert.ToBoolean(dr[3]),
Birthday = Convert.ToDateTime(dr[4]),
Phone = dr[5].ToString(),
Email = dr[6].ToString(),
Natoin = dr[7].ToString(),
Area = dr[8].ToString(),
};
List.Add(u);
}
}
con.Close();
return List;
}
以上是关于组合查询 分页的主要内容,如果未能解决你的问题,请参考以下文章