webform 分页、组合查询综合使用
界面:
<%@ 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=">=">大于等于</asp:ListItem>
<asp:ListItem Value="<=">小于等于</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> 
<asp:LinkButton ID="btn_Prev" runat="server">上一页</asp:LinkButton> 
<asp:LinkButton ID="btn_Next" runat="server">下一页</asp:LinkButton> 
<asp:LinkButton ID="btn_Last" runat="server">尾页</asp:LinkButton> 
<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 分页、组合查询综合使用的更多相关文章
- webform:分页组合查询
一个简单的分页组合查询页面 /// <summary> /// 查询方法 /// </summary> /// <param name="tsql"& ...
- ASP.NETMVC4 分页组合查询解决方法
本人新手刚在webform转到mvc 像linq ef啥的,都是不会的不行不行的,不会就问群友,找资料 今天本屌遇到了一个分页组合查询的问题,解决了2个小时,把代码共享给大家 话不多话,直接上代 ...
- Linq组合查询与分页组合查询结合
1.组合查询 <div>姓名:<asp:TextBox ID="T1" runat="server"></asp:TextBox& ...
- 【2017-06-02】Linq高级查询,实现分页组合查询。
1.以XXX开头 2.以XXX结尾 3.模糊查询 4.求个数 5.求最大值 6.求最小值 7.求平均值 8.求和 9.升序 10.降序 11.分页 Skip()跳过多少条 Take()取多少条 12. ...
- linq分页组合查询
一.linq高级查 1.模糊查(字符串包含) 1 public List<User> Select(string name) 2 { 3 return con.User.Where(r = ...
- Webform--LinQ 分页组合查询
一.linq高级查 1.模糊查(字符串包含) public List<User> Select(string name) { return con.User.Where(r => r ...
- ASP.NET 分页+组合查询 练习
分页和组合查询都是通过拼接SQL语句到数据库查询进行实现 到汽车表(car)中查询 ,汽车表选取了“编号 code”,“车名 name”,“日期 time”,“油耗 oil ”,“马力 powers” ...
- Webform(Linq高级查、分页、组合查询)
一.linq高级查 1.模糊查(包含) 1 public List<User> Select(string name) 2 { 3 return con.User.Where(r => ...
- Webform(分页与组合查询配合使用)
1.封装实体类 2.写查询方法 //SubjectData类 public List<Subject> Select(string name) { List<Subject> ...
随机推荐
- UFLDL 教程三总结与答案
主成分分析(PCA)是一种能够极大提升无监督特征学习速度的数据降维算法.更重要的是,理解PCA算法,对实现白化算法有很大的帮助,很多算法都先用白化算法作预处理步骤.这里以处理自然图像为例作解释. 1. ...
- InstallSheild的一些常量
在用InstallShield制作安装包的时候,我们经常需要用到一些常量,弄清楚这些常量的具体含义,可以方便我们灵活使用脚本. TARGETDIR 默认安装路径,在安装过程中用户可手动更改.如:安装 ...
- 我的CodeF水A题之路
Codeforces Round #359 (Div. 2) A. Free Ice Cream 题目链接:http://www.codeforces.com/problemset/problem/6 ...
- useradd与adduser的区别
useradd与adduser都是创建新的用户 在CentOs下useradd与adduser是没有区别的都是在创建用户,在home下自动创建目录,没有设置密码,需要使用passwd命令修改密码. 而 ...
- 利用yeoman快速搭建React+webpack+es6脚手架
自从前后端开始分离之后,前端项目工程化也显得越来越重要了,之前写过一篇搭建基于Angular+Requirejs+Grunt的前端项目教程,有兴趣的可以点这里去看 但是有些项目可以使用这种方式,但有些 ...
- html5 websocket 实时日志推送
http://blog.csdn.net/neutrojan/article/details/46119645
- scikit-learn算法选择图
图片来自sklearn官网 最近事情弄完一部分了,继续开始python data science!
- Android-Menu [使用C# And Java实现]
本篇是对安卓菜单使用编程方式实现,当然可以使用XML的方式完成同样的功能,基本Java和C#写法都是一致的,所以使用XML的方式在本篇中使用Java演示,需要注意的是,对于如果不是VS开发的话,那么资 ...
- windowsservice
1.创建 windows服务 项目 文件 -> 新建项目 -> 已安装的模板 -> Visual C# -> windows ,在右侧窗口选择"windows 服务 ...
- 在Spring中轻松写日志
最近觉得写的一点代码(JAVA),还觉得颇为自得,贡献出来供大家参考. 首先,先上代码: @Controller public class Controller1{ @WriteLog(value = ...