组合查询 分页

分页:

   当前第<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;
        }
				
时间: 2024-12-09 18:49:03

组合查询 分页的相关文章

组合查询分页代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server&qu

webform:分页组合查询

一个简单的分页组合查询页面 /// <summary> /// 查询方法 /// </summary> /// <param name="tsql">SQL语句</param> /// <param name="hh">哈希表</param> /// <returns></returns> public List<Goods> Select(string un

WebForm 分页与组合查询

1.封装实体类 2.写查询方法 //SubjectData类 public List<Subject> Select(string name) { List<Subject> list = new List<Subject>(); cmd.CommandText = "select *from Subject where SubjectName like @a "; cmd.Parameters.Clear(); cmd.Parameters.Add

ADO.NET组合查询及其分页合并

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server&quo

组合查询和分页

组合查询和分页的核心思想就是拼接SQL语句,根据自己想要显示的内容写出SQL语句查询显示. 组合查询和分页小练习: Model: 1 private string _Code; 2 /// <summary> 3 /// 编号 4 /// </summary> 5 public string Code 6 { 7 get { return _Code; } 8 set { _Code = value; } 9 } 10 private string _Name; 11 /// &l

Webform--LinQ 分页组合查询

一.linq高级查 1.模糊查(字符串包含)   2.查开头(StartsWith,以XX开头) public List<User> Select(string name) { return con.User.Where(r => r.UserName.StartsWith(name)).ToList(); } 3.查结尾 public List<User> Select(string name) { return con.User.Where(r => r.UserN

webform组合查询和分页

1.组合查询(1)数据访问类 //参数1:SQL语句 参数2:哈希表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

Webform(Linq高级查、分页、组合查询)

Linq高级查:字符串:模糊查(包含):con.car.Where(r => r.name.Contains(name)).ToList();以什么开头:con.car.Where(r => r.name.StartsWith(name)).ToList();以什么结尾:con.car.Where(r => r.name.EndsWith(name)).ToList(); 数值:个数:.Count()最大值:con.car.Max(r => r.price);最小值:con.car

Webform(分页与组合查询配合使用)

1.封装实体类 2.写查询方法 //SubjectData类 public List<Subject> Select(string name) { List<Subject> list = new List<Subject>(); cmd.CommandText = "select *from Subject where SubjectName like @a "; cmd.Parameters.Clear(); cmd.Parameters.Add