界面:

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 <!DOCTYPE html>

 <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
名称:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
油耗:<asp:DropDownList ID="DropDownList3" runat="server" EnableViewState="True">
<asp:ListItem Value="=">等于</asp:ListItem>
<asp:ListItem Value="&gt;=">大于等于</asp:ListItem>
<asp:ListItem Value="&lt;=">小于等于</asp:ListItem>
</asp:DropDownList><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
价格:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>-<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="提交" /><br /><br />
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table style="background-color: blue; width: 100%; text-align:center">
<thead>
<tr style="color: white;">
<td>编号</td>
<td>名称</td>
<td>品牌</td>
<td>上市时间</td>
<td>油耗</td>
<td>动力</td>
<td>排量</td>
<td>价格</td>
<td>图片</td>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color: #808080">
<td><%#Eval("Code") %></td>
<td><%#Eval("Name") %></td>
<td><%#Eval("Brand") %></td>
<td><%#Eval("Time") %></td>
<td><%#Eval("Oil") %></td>
<td><%#Eval("Power") %></td>
<td><%#Eval("Exhaust") %></td>
<td><%#Eval("Price") %></td>
<td><%#Eval("Pic") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
<div style="width: 50%; position: relative; float: left; height: 25px; line-height: 25px; text-indent: 20px;">

<asp:Label ID="Label_Sum" runat="server" Text="Label"></asp:Label>
条记录,共
<asp:Label ID="Label_Maxpage" runat="server" Text="Label"></asp:Label>
页,第
<asp:Label ID="Label_Nowpage" runat="server" Text="Label"></asp:Label>
页|每页
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" ></asp:DropDownList>
条记录
</div>
<div style="width: 30%; position: relative; float: right; height: 25px; line-height: 25px;">
<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_Last" runat="server">尾页</asp:LinkButton>&nbsp
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="跳转" />
</div>
</div>
</form>
</body>
</html>

界面:Default.aspx

后台:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; /// <summary>
/// Car 的摘要说明
/// </summary>
public class Car
{
public Car()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public string Code { get; set; }
public string Name { get; set; }
public string Brand { get; set; }
public DateTime Time { get; set; }
public decimal Oil { get; set; }
public int Power { get; set; }
public decimal Exhaust { get; set; }
public decimal Price { get; set; }
public string Pic { get; set; }
}

封装实体类

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Collections; /// <summary>
/// CarData 的摘要说明
/// </summary>
public class CarData
{
SqlConnection conn = null;
SqlCommand cmd = null;
public CarData()
{
conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=123");
cmd = conn.CreateCommand();
} public List<Car> Select()
{
List<Car> clist = new List<Car>();
cmd.CommandText = "select *from Car"; conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
Car c = new Car();
c.Code = dr[].ToString();
c.Name = dr[].ToString();
c.Brand = dr[].ToString();
c.Time = Convert.ToDateTime(dr[]);
c.Oil = Convert.ToDecimal(dr[]);
c.Power = Convert.ToInt32(dr[]);
c.Exhaust = Convert.ToInt32(dr[]);
c.Price = Convert.ToDecimal(dr[]);
c.Pic = dr[].ToString(); clist.Add(c);
}
}
conn.Close();
return clist;
} public List<Car> Select(int count,int nowpage)
{
List<Car> clist = new List<Car>();
cmd.CommandText = "select top "+count+" *from Car where Code not in (select top "+((nowpage-)*count)+" Code from Car) "; conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
Car c = new Car();
c.Code = dr[].ToString();
c.Name = dr[].ToString();
c.Brand = dr[].ToString();
c.Time = Convert.ToDateTime(dr[]);
c.Oil = Convert.ToDecimal(dr[]);
c.Power = Convert.ToInt32(dr[]);
c.Exhaust = Convert.ToInt32(dr[]);
c.Price = Convert.ToDecimal(dr[]);
c.Pic = dr[].ToString(); clist.Add(c);
}
}
conn.Close();
return clist;
}
public List<Car> Select(string sql, Hashtable hat )
{
List<Car> clist = new List<Car>();
cmd.CommandText = sql;
cmd.Parameters.Clear(); foreach (string s in hat.Keys)
{
cmd.Parameters.AddWithValue(s, hat[s]);
} conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
Car c = new Car();
c.Code = dr[].ToString();
c.Name = dr[].ToString();
c.Brand = dr[].ToString();
c.Time = Convert.ToDateTime(dr[]);
c.Oil = Convert.ToDecimal(dr[]);
c.Power = Convert.ToInt32(dr[]);
c.Exhaust = Convert.ToInt32(dr[]);
c.Price = Convert.ToDecimal(dr[]);
c.Pic = dr[].ToString(); clist.Add(c);
}
}
conn.Close();
return clist;
}
}

数据访问类

 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 _Default : System.Web.UI.Page
{
Hashtable has = new Hashtable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList2.Items.Clear();
string tsql2 = "select *from Car";
for (int i = ; i <= Sum(tsql2,has); i++)
{
DropDownList2.Items.Add(new ListItem(i.ToString(), i.ToString()));
} DropDownList2.SelectedValue = "";//默认每页五条 Change();
int count = Convert.ToInt32(DropDownList2.SelectedValue);//每页的数据条数
//Repeater1.DataSource = new CarData().Select(count, 1);
//Repeater1.DataBind();
} btn_First.Click += btn_First_Click;//首页
btn_Prev.Click += btn_Prev_Click;//上一页
btn_Next.Click += btn_Next_Click;//下一页
btn_Last.Click += btn_Last_Click;//尾页
Button1.Click += Button1_Click;//跳转按钮
DropDownList2.SelectedIndexChanged += DropDownList2_SelectedIndexChanged;//每页数据条数发生改变
Button2.Click += Button2_Click;
}
//每页数据条数发生改变时执行
private void Change()
{
string tsql;//拼接查询前count条数据的语句
string tsql2;//查询所有的语句
string tj;//用于分页查询与sql等拼接
Tsql(out tsql, out tsql2, out tj);
//int count = Convert.ToInt32(DropDownList2.SelectedValue);//每页的数据条数
Label_Sum.Text = Sum(tsql2, has).ToString();//总数据条数
Label_Maxpage.Text = MaxPage(tsql2, has).ToString();//总页数
Label_Nowpage.Text = "";//当前页
Repeater1.DataSource = new CarData().Select(tsql, has);
Repeater1.DataBind(); DropDownList1.Items.Clear();
for (int i = ; i <= MaxPage(tsql2, has); i++)
{
DropDownList1.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
btn_First.Enabled = false;
btn_Prev.Enabled = false;
btn_Next.Enabled = true;
btn_Last.Enabled = true;
}
/// <summary>
/// 返回所有条件的Tsql语句
/// </summary>
/// <param name="tsql">拼接查询前count条数据的语句</param>
/// <param name="tsql2">查询所有的语句</param>
/// <param name="tj">用于分页查询与sql等拼接</param>
private void Tsql(out string tsql, out string tsql2, out string tj)
{
int count = Convert.ToInt32(DropDownList2.SelectedValue);//每页的数据条数
tsql = "select top " + count + " *from Car";
tsql2 = "select *from Car";
tj = "";
//判断文本框中是否有内容需要查询
if (TextBox1.Text.Trim().Length > )
{//如果有内容,那么就拼接到Tsql语句中去
tsql += " where name like @name";
tsql2 += " where name like @name";
tj += " where name like @name";
has.Add("@name", "%" + TextBox1.Text.Trim().ToUpper() + "%");
}
else
{
tsql += " where 1=1";
tsql2 += " where 1=1";
tj += " where 1=1";
}
if (TextBox2.Text.Trim().Length > )
{
tsql += " and oil " + DropDownList3.SelectedValue + "@oil";
tsql2 += " and oil " + DropDownList3.SelectedValue + "@oil";
tj += " and oil " + DropDownList3.SelectedValue + "@oil";
has.Add("@oil", TextBox2.Text.Trim());
}
else
{
tsql += " and 1=1";
tsql2 += " and 1=1";
tj += " and 1=1";
}
if (TextBox3.Text.Trim().Length > )
{
tsql += " and price>=@price1";
tsql2 += " and price>=@price1";
tj += " and price>=@price1";
has.Add("@price1", TextBox3.Text.Trim());
}
else
{
tsql += " and 1=1";
tsql2 += " and 1=1";
tj += " and 1=1";
}
if (TextBox4.Text.Trim().Length > )
{
tsql += " and price<=@price2";
tsql2 += " and price<=@price2";
tj += " and price<=@price2";
has.Add("@price2", TextBox4.Text.Trim());
}
} //组合查询按钮
void Button2_Click(object sender, EventArgs e)
{ string tsql;//拼接查询前count条数据的语句
string tsql2;//查询所有的语句
string tj;
Tsql(out tsql, out tsql2, out tj);
Repeater1.DataSource = new CarData().Select(tsql, has);//数据指向
Repeater1.DataBind();
Label_Sum.Text = Sum(tsql2, has).ToString();//总数据条数
Label_Maxpage.Text = MaxPage(tsql2, has).ToString();//总页数
Label_Nowpage.Text = "";//当前页 DropDownList1.Items.Clear();
for (int i = ; i <= MaxPage(tsql2, has); i++)
{
DropDownList1.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
btn_First.Enabled = false;
btn_Prev.Enabled = false;
btn_Next.Enabled = true;
btn_Last.Enabled = true; if (Sum(tsql2, has) <= Convert.ToInt32(DropDownList2.SelectedValue))
{
btn_Next.Enabled = false;
btn_Last.Enabled = false;
}
if (Sum(tsql2, has)==)
{
Label_Nowpage.Text = "";
} string mi = DropDownList2.SelectedValue;
DropDownList2.Items.Clear();
for (int i = ; i <= Sum(tsql2, has); i++)
{
DropDownList2.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
if (Sum(tsql, has) <= Convert.ToUInt32(mi))
{
DropDownList2.SelectedValue = Sum(tsql, has).ToString();//默认每页条数
}
} //每页数据条数发生改变
void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
Change();
if (DropDownList2.SelectedValue == "")
{
btn_Next.Enabled = false;
btn_Last.Enabled = false;
}
} //跳转按钮
void Button1_Click(object sender, EventArgs e)
{
string tsql;//拼接查询前count条数据的语句
string tsql2;//查询所有的语句
string tj;
Tsql(out tsql, out tsql2, out tj);
int count = Convert.ToInt32(DropDownList2.SelectedValue);//每页的数据条数 tsql += " and Code not in(select top " + (count * (Convert.ToInt32(DropDownList1.SelectedValue) - )) + " Code from Car " + tj + ")";
//按照页数获取绑定数据
Repeater1.DataSource = new CarData().Select(tsql,has);
Repeater1.DataBind();
//修改显示页数
Label_Nowpage.Text = DropDownList1.SelectedValue;
if (Convert.ToInt32(Label_Nowpage.Text) <= )//若为首页禁用上一页和首页按钮
{
btn_First.Enabled = false;
btn_Prev.Enabled = false;
btn_Next.Enabled = true;
btn_Last.Enabled = true;
}
if (Convert.ToInt32(Label_Nowpage.Text) >= MaxPage(tsql2, has))//若为最后一页禁用下一页和尾页按钮
{
btn_Next.Enabled = false;
btn_Last.Enabled = false;
btn_First.Enabled = true;
btn_Prev.Enabled = true;
}
} //首页
void btn_First_Click(object sender, EventArgs e)
{
string tsql;//拼接查询前count条数据的语句
string tsql2;//查询所有的语句
string tj;//用于分页查询与sql等拼接
Tsql(out tsql, out tsql2, out tj);
int count = Convert.ToInt32(DropDownList2.SelectedValue);//每页的数据条数 tsql += " and Code not in(select top 0 Code from Car " + tj + ")";
//按照页数获取绑定数据
Repeater1.DataSource = new CarData().Select(tsql,has);
Repeater1.DataBind();
//修改显示页数
Label_Nowpage.Text = "";
//当前为首页禁用上一页和首页按钮
btn_First.Enabled = false;
btn_Prev.Enabled = false; btn_Next.Enabled = true;
btn_Last.Enabled = true;
} //上一页
void btn_Prev_Click(object sender, EventArgs e)
{
string tsql;//拼接查询前count条数据的语句
string tsql2;//查询所有的语句
string tj;//用于分页查询与sql等拼接
Tsql(out tsql, out tsql2, out tj);
int count = Convert.ToInt32(DropDownList2.SelectedValue);//每页的数据条数
//获取当前页,计算上一页页数
int nextpage = Convert.ToInt32(Label_Nowpage.Text) - ; tsql += " and Code not in(select top " + (count * (nextpage - )) + " Code from Car " + tj + ")";
//按照页数获取绑定数据
Repeater1.DataSource = new CarData().Select(tsql, has);
Repeater1.DataBind();
//修改显示页数
Label_Nowpage.Text = nextpage.ToString();
if (nextpage <= )//若为首页禁用上一页和首页按钮
{
btn_First.Enabled = false;
btn_Prev.Enabled = false;
}
btn_Next.Enabled = true;
btn_Last.Enabled = true;
}
//下一页
void btn_Next_Click(object sender, EventArgs e)
{
string tsql;//拼接查询前count条数据的语句
string tsql2;//查询所有的语句
string tj;//用于分页查询与sql等拼接
Tsql(out tsql, out tsql2, out tj);
int count = Convert.ToInt32(DropDownList2.SelectedValue);//每页的数据条数
//获取当前页,计算下一页页数
int nextpage = Convert.ToInt32(Label_Nowpage.Text) + ; tsql += " and Code not in(select top " + (count * (nextpage - )) + " Code from Car " + tj + ")";
//按照页数获取绑定数据
Repeater1.DataSource = new CarData().Select(tsql, has);
Repeater1.DataBind();
//修改显示页数
Label_Nowpage.Text = nextpage.ToString();
if (nextpage >= MaxPage(tsql2, has))//若为最后一页禁用下一页和尾页按钮
{
btn_Next.Enabled = false;
btn_Last.Enabled = false;
}
btn_First.Enabled = true;
btn_Prev.Enabled = true;
} //尾页
void btn_Last_Click(object sender, EventArgs e)
{
string tsql;//拼接查询前count条数据的语句
string tsql2;//查询所有的语句
string tj;//用于分页查询与sql等拼接
Tsql(out tsql, out tsql2, out tj);
int count = Convert.ToInt32(DropDownList2.SelectedValue);//每页的数据条数 tsql += " and Code not in(select top " + (count * (MaxPage(tsql2,has) - )) + " Code from Car " + tj + ")";
//按照页数获取绑定数据
Repeater1.DataSource = new CarData().Select(tsql,has);
Repeater1.DataBind();
//修改显示页数
Label_Nowpage.Text = MaxPage(tsql2, has).ToString();
//若为最后一页禁用下一页和尾页按钮
btn_Next.Enabled = false;
btn_Last.Enabled = false; btn_First.Enabled = true;
btn_Prev.Enabled = true;
} //总的数据条数
public int Sum(string tsql,Hashtable has)
{
List<Car> clist = new CarData().Select(tsql,has);
int a = clist.Count;
return a;
} //总页数
public int MaxPage(string tsql, Hashtable has)
{
int count = Convert.ToInt32(DropDownList2.SelectedValue);//每页的数据条数
List<Car> clist = new CarData().Select(tsql, has);
int a = clist.Count;
int end = Convert.ToInt32(Math.Ceiling(a / (count * 1.0)));
return end;
}
}

Default.aspx.cs

webform 分页、组合查询综合使用的更多相关文章

  1. webform:分页组合查询

    一个简单的分页组合查询页面 /// <summary> /// 查询方法 /// </summary> /// <param name="tsql"& ...

  2. ASP.NETMVC4 分页组合查询解决方法

    本人新手刚在webform转到mvc   像linq  ef啥的,都是不会的不行不行的,不会就问群友,找资料 今天本屌遇到了一个分页组合查询的问题,解决了2个小时,把代码共享给大家 话不多话,直接上代 ...

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

    1.组合查询 <div>姓名:<asp:TextBox ID="T1" runat="server"></asp:TextBox& ...

  4. 【2017-06-02】Linq高级查询,实现分页组合查询。

    1.以XXX开头 2.以XXX结尾 3.模糊查询 4.求个数 5.求最大值 6.求最小值 7.求平均值 8.求和 9.升序 10.降序 11.分页 Skip()跳过多少条 Take()取多少条 12. ...

  5. linq分页组合查询

    一.linq高级查 1.模糊查(字符串包含) 1 public List<User> Select(string name) 2 { 3 return con.User.Where(r = ...

  6. Webform--LinQ 分页组合查询

    一.linq高级查 1.模糊查(字符串包含) public List<User> Select(string name) { return con.User.Where(r => r ...

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

    分页和组合查询都是通过拼接SQL语句到数据库查询进行实现 到汽车表(car)中查询 ,汽车表选取了“编号 code”,“车名 name”,“日期 time”,“油耗 oil ”,“马力 powers” ...

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

    一.linq高级查 1.模糊查(包含) 1 public List<User> Select(string name) 2 { 3 return con.User.Where(r => ...

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

    1.封装实体类 2.写查询方法 //SubjectData类 public List<Subject> Select(string name) { List<Subject> ...

随机推荐

  1. Camtasia 录屏说明

    准备好要录制的屏幕或网页,在即将播放的位置暂停住. 从开始菜单位置“TechSmith”启动Camtasia Recorder 8,其界面如下所示: 注意,要录制系统声音,须在Recorded inp ...

  2. marquee实现文字移动效果;js+div实现文字无缝移动效果

    1.marquee实现文字移动: <marquee width="220px;" scrollamount="5" onmouseover="t ...

  3. centos 7 配置iptables

    环境:阿里云ECS.centos 7 一.防火墙配置 不知道为什么,云主机没有开启firewall 或iptables,记录一下配置iptables防火墙的步骤 1.检测并关闭firewall sys ...

  4. Restful API

    http://www.ruanyifeng.com/blog/2011/09/restful 参考资料:-------以网络为基础的应用软件的架构设计. Restful API的设计与实践 字数218 ...

  5. while用法一例

    package com.chongrui.test;/*while用法一例 * *///import java.util.Scanner;public class TypeConvertion { p ...

  6. gulp使用过程中出现的问题

    在使用gulp的过程中,最容易出现错误的地方就是在安装本地的gulp的时候,错误的原因有: 1.本来是局部安装gulp,但使用命令时还带-g. 2.忘记在局部安装gulp. 以上两种情况出错时会报错, ...

  7. (3)WebApi客户端调用

    1.创建一个应用台控制程序,可以把Model的引用,用下面的方法拖拽上来(解决方案里没有这个文件,只是这个文件的引用)  2.Program.cs using System; using System ...

  8. llinux常用命令

    (1)Ctrl+alt+(1-6)可调用控制台程序 (2)date---可看时间日期 (3)date 月日时分年 ------可修改时间 (4)useradd-------新建用户 (5)passwd ...

  9. centos+scala2.11.4+hadoop2.3+spark1.3.1环境搭建

    一.Java安装 1.安装包准备: 首先到官网下载jdk,http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads- ...

  10. Node使用multiparty包上传文件

    var multiparty = require('multiparty'); var http = require('http'); var util = require('util'); var ...