分页查询组合查询

Posted 尤痴武

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分页查询组合查询相关的知识,希望对你有一定的参考价值。

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));//返回最大或等于的整数
    }
}
复制代码

组合查询:

 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=" + +;
    }
}

以上是关于分页查询组合查询的主要内容,如果未能解决你的问题,请参考以下文章

2017-5-24 分页查询,组合查询

分页查询/组合查询

Linq组合查询与分页组合查询结合

分页查询组合查询

webform:分页组合查询

组合查询与分页查询结合应用