分页查询/组合查询

 1 </组合查询
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 [email protected]";
            }
            else
            {
                Tsql += " where [email protected]";
            }
            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=" + +;
    }
}
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7
 8 public partial class fenye : System.Web.UI.Page
 9 {
10     protected void Page_Load(object sender, EventArgs e)
11     {
12         if (!IsPostBack)
13         {
14             Repeater1.DataSource = new ChainDA().Select(Pagcount, 1);//默认是5条数据,第一页
15             Repeater1.DataBind();
16             Label1.Text = "1";//打开就显示当前第1页
17
18             for (int i = 1; i <= Max(); i++)
19             {
20             //每一条数据都是ListItem
21                 ListItem li = new ListItem(i.ToString(),i.ToString());
22                 //复合控件
23                 DropDownList1.Items.Add(li);
24             }
25
26
27         }
28         Label2.Text = Max().ToString();//显示总共多少页
29
30         btn_first.Click += btn_first_Click;//首页按钮
31         btn_end.Click += btn_end_Click;//末页按钮
32         btn_prev.Click += btn_prev_Click;//上一页按钮
33         btn_next.Click += btn_next_Click;//下一页按钮
34         Button1.Click += Button1_Click;//点击跳转按钮
35         DropDownList1.SelectedIndexChanged += Button1_Click;
36     }
37
38     void Button1_Click(object sender, EventArgs e)
39     {//跳转按钮
40         int a = Convert.ToInt32(DropDownList1.SelectedItem.Text);
41         Label1.Text = a.ToString();
42         Repeater1.DataSource = new ChainDA().Select(Pagcount, a);
43         Repeater1.DataBind();//重新赋值
44     }
45     int Pagcount = 10;//每页显示多少条
46
47     //下一页按钮
48     void btn_next_Click(object sender, EventArgs e)
49     {
50         int num = Convert.ToInt32(Label1.Text);//
51         num++;//下一页
52         if (num >= Max())//超过最大页就不执行了,下面语句也不执行
53         {
54             return;
55         }
56         Label1.Text = num.ToString();
57         Repeater1.DataSource = new ChainDA().Select(Pagcount, num);//显示Pagcount条数据,num是下一页
58         Repeater1.DataBind();//重新赋值
59     }
60     //上一页按钮
61     void btn_prev_Click(object sender, EventArgs e)
62     {
63         int num = Convert.ToInt32(Label1.Text);//
64         num--;//上一页
65         if (num < 1)//超过最小页就不执行了,下面语句也不执行
66         {
67             return;
68         }
69         Label1.Text = num.ToString();
70         Repeater1.DataSource = new ChainDA().Select(Pagcount, num);//显示Pagcount条数据,num是上一页
71         Repeater1.DataBind();//重新赋值
72     }
73     //末页按钮
74     void btn_end_Click(object sender, EventArgs e)
75     {
76         Repeater1.DataSource = new ChainDA().Select(Pagcount, Max());//显示Pagcount条数据,Max()是最大页
77         Repeater1.DataBind();//重新赋值
78         Label1.Text = Max().ToString();
79     }
80     //首页按钮
81     void btn_first_Click(object sender, EventArgs e)
82     {
83
84         Repeater1.DataSource = new ChainDA().Select(Pagcount, 1);//显示Pagcount条数据,1是首页
85         Repeater1.DataBind();//重新赋值
86         Label1.Text ="1";
87     }
88     private int Max()//最大页
89     {
90         int count = new ChainDA().Select().Count;
91         double aa= count/(Pagcount*1.0);//所有的数据除以每页显示的条数就是一共多少页
92         return Convert.ToInt32(Math.Ceiling(aa));//返回最大或等于的整数
93     }
94 }
asp:Repeater>
 2         <br />
 3         当前第[<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>]页,
 4         共[<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>]页 &nbsp;
 5         <asp:LinkButton ID="btn_first" runat="server">首页</asp:LinkButton>&nbsp;
 6         <asp:LinkButton ID="btn_prev" runat="server">上一页</asp:LinkButton>&nbsp;
 7         <asp:LinkButton ID="btn_next" runat="server">下一页</asp:LinkButton>&nbsp;
 8         <asp:LinkButton ID="btn_end" runat="server">末页</asp:LinkButton>&nbsp;
 9         <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"></asp:DropDownList>
10         <asp:Button ID="Button1" runat="server" Text="跳转" />
时间: 2024-12-28 00:32:46

分页查询/组合查询的相关文章

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

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

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

2017-6-2 Linq 高级查询 (分页和组合查)、集合取交集

1.linq分页和组合查询:(用项目实战来解释) <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <he

ASP.NET 分页+组合查询 练习

分页和组合查询都是通过拼接SQL语句到数据库查询进行实现 到汽车表(car)中查询 ,汽车表选取了“编号 code”,“车名 name”,“日期 time”,“油耗 oil ”,“马力 powers”这几列 分页查询语句:每页只显示五条数据 查询前五条数据: select top 5 from car 点击下一页按钮:select top 5 from car where code not in(select top 5 code from car)//第二页的时候通过编号,查询前五条之后的数据

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

分页查询   组合查询 1.用到的实体类: public class ab { public int Ids { get; set; } public string UserName { get; set; } public string PassWord { get; set; } public string Title { get; set; } public string Score { get; set; } public string Class { get; set; } } 2.数

webform:分页组合查询

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

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