• 分页查询、组合查询


         </asp:Repeater>
            <br />
            当前第[<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>]页,
            共[<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>]页 &nbsp;
            <asp:LinkButton ID="btn_first" runat="server">首页</asp:LinkButton>&nbsp;
            <asp:LinkButton ID="btn_prev" runat="server">上一页</asp:LinkButton>&nbsp;
            <asp:LinkButton ID="btn_next" runat="server">下一页</asp:LinkButton>&nbsp;
            <asp:LinkButton ID="btn_end" runat="server">末页</asp:LinkButton>&nbsp;
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"></asp:DropDownList>
            <asp:Button ID="Button1" runat="server" Text="跳转" />
    View Code
    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));//返回最大或等于的整数
        }
    }

    组合查询:

     public List<Car> Select(string Tsql,Hashtable hs)//直接传sql语句
        {
            List<Car> list = new List<Car>();
    
            cmd.CommandText = Tsql;
            cmd.Parameters.Clear();//哈希表集合
    
            foreach (string k in hs.Keys)
            {
                cmd.Parameters.Add(k, hs[k]);
            }
    
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
    
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    Car c = new Car()
                    {
                        ids=dr[0].ToString(),
                        code=dr[1].ToString(),
                        name=dr[2].ToString(),
                        brand=dr[3].ToString(),
                        time= Convert.ToDateTime(dr[4]),
                        oil = Convert.ToDecimal(dr[5]),
                        powers=Convert.ToInt32(dr[6]),
                        exhaust=Convert.ToInt32(dr[7]),
                        price=Convert.ToDecimal(dr[8]),
                        pic = dr[9].ToString()
                    };
                    list.Add(c);
                }
            }
            conn.Close();
            return list;
        }
    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=" + +;
        }
    }
  • 相关阅读:
    html页面禁止自动填充浏览器记住的密码
    未能加载文件或程序集“System.Data.SQLite.DLL”或它的某一个依赖项
    DotNetMQ的一个小demo
    js 排列 组合 的一个简单例子
    js 生成笛卡尔积
    290.单词模式。给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循相同的模式。(c++方法)
    递归与迭代的联系以及优缺点(以c++为例)
    leetcode344 反转字符串 c++实现
    面试题:编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回空字符串 ""。(c++实现)
    LeetCode题库13. 罗马数字转整数(c++实现)
  • 原文地址:https://www.cnblogs.com/yp11/p/5917101.html
Copyright © 2020-2023  润新知