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("@a","%"+name+"%");
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
Subject s = new Subject();
s.SubjectCode = dr[].ToString();
s.SubjectName = dr[].ToString();
list.Add(s);
}
}
conn.Close(); return list;
}
//StudentData类
/// <summary>
/// 查询方法
/// </summary>
/// <param name="tsql">SQL语句</param>
/// <param name="hh">哈希表</param>
/// <returns></returns>
public List<Student> Select(string tsql,Hashtable hh)
{
List<Student> list = new List<Student>();
cmd.CommandText = tsql;
cmd.Parameters.Clear();
foreach( string s in hh.Keys)
{
cmd.Parameters.Add(s,hh[s]);
}
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
Student s = new Student();
s.Code = dr[].ToString();
s.Name = dr[].ToString();
s.Sex = Convert.ToBoolean(dr[]);
s.Birthday = Convert.ToDateTime(dr[]);
s.SubjectCode = dr[].ToString();
s.Nation = dr[].ToString();
list.Add(s);
}
}
conn.Close();
return list;
}
3.Page_Load部分,最大页方法
int PageCount = ; //每页显示条数
Hashtable hs = new Hashtable();
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
string tsql = "select top "+PageCount+" *from Student";//查询前PageCount条数据
//Repeater1数据源指向
List<Student> list = new StudentData().Select(tsql,hs);
Repeater1.DataSource = list;
Repeater1.DataBind();
Label2.Text = "";//第一页
//获取最大页
string sql = "select *from Student";
Label3.Text = MaxPageNumber(sql,hs).ToString();
for (int i = ; i <= MaxPageNumber(sql,hs); i++)//给可快速跳转列表框赋值
{
DropDownList2.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
}
}
Page_Load
public int MaxPageNumber(string sql, Hashtable hs)
{
List<Student> list = new StudentData().Select(sql, hs);//查询所有数据 double de = list.Count / (PageCount * 1.0); int aa = Convert.ToInt32(Math.Ceiling(de));//取上限
return aa;
}
获取最大页
4.根据组合查询拼接语句方法
/// <summary>
///
/// </summary>
/// <param name="sql">拼接查询前PageCount条数据的语句</param>
/// <param name="sql2">查询所有的语句</param>
/// <param name="tj">用于分页查询与sql等拼接</param>
/// <param name="count">判断前几项是否为空</param>
private void Tsql(out string sql, out string sql2,out string tj,out int count)
{
count = ;
sql = "select top " + PageCount + " *from Student";
sql2 = "select *from Student";
tj = "";
//性别不为空
if (!string.IsNullOrEmpty(tb_sex.Text.Trim()))
{//判断输入的是男是女,其它输入默认为未输入内容
if (tb_sex.Text.Trim() == "男")
{
sql += " where Sex = @a";
sql2 += " where Sex = @a";
tj += " where Sex = @a";
hs.Add("@a", "true");
count++;
}
else if (tb_sex.Text.Trim() == "女")
{
sql += " where Sex = @a";
sql2 += " where Sex = @a";
tj += " where Sex = @a";
hs.Add("@a", "false");
count++;
}
}
//年龄不为空
if (!string.IsNullOrEmpty(tb_age.Text.Trim()))
{
int a = DateTime.Now.Year;//获取当前时间的年
try//确保输入的是数字
{
int ag = Convert.ToInt32(tb_age.Text.Trim());
int g = a - ag;
DateTime d = Convert.ToDateTime(g.ToString() + "-1-1");
if (DropDownList3.SelectedValue == ">=")//小于或等于您输入的年龄,即大于或等于某个时间
{
if (count == )//前面的一项未输入(性别)
{
sql += " where Birthday " + DropDownList3.SelectedValue + "@b";
sql2 += " where Birthday " + DropDownList3.SelectedValue + "@b";
tj += " where Birthday " + DropDownList3.SelectedValue + "@b";
}
else
{
sql += " and Birthday " + DropDownList3.SelectedValue + "@b";
sql2 += " and Birthday " + DropDownList3.SelectedValue + "@b";
tj += " and Birthday " + DropDownList3.SelectedValue + "@b";
}
hs.Add("@b", d);
}
else//大于或等于您输入的年龄,即小于或等于某个时间
{
DateTime dd = Convert.ToDateTime(g.ToString() + "-12-31");
if (count == )
{
sql += " where Birthday " + DropDownList3.SelectedValue + "@b";
sql2 += " where Birthday " + DropDownList3.SelectedValue + "@b";
tj += " where Birthday " + DropDownList3.SelectedValue + "@b";
}
else
{
sql += " and Birthday " + DropDownList3.SelectedValue + "@b";
sql2 += " and Birthday " + DropDownList3.SelectedValue + "@b";
tj += " and Birthday " + DropDownList3.SelectedValue + "@b";
}
hs.Add("@b", dd);
}
count++;
}
catch
{
}
}
if (!string.IsNullOrEmpty(tb_s.Text.Trim()))//判断专业是否为空
{
List<Subject> li = new SubjectData().Select(tb_s.Text.Trim());//调用查询方法模糊查询专业
if (li.Count <= )//未查到数据
{
}
else//查到数据
{
int cou = ;//用于查到的为多条数据
foreach (Subject ub in li)
{
if (li.Count == )//只查到一条数据
{
if (count == )//性别与年龄输入框都未输入内容
{
sql += " where SubjectCode =@c";
sql2 += " where SubjectCode =@c";
tj += " where SubjectCode =@c";
}
else
{
sql += " and SubjectCode =@c";
sql2 += " and SubjectCode =@c";
tj += " and SubjectCode =@c";
}
hs.Add("@c", ub.SubjectCode);
cou++;
count++;
}
else//查到多条数据
{
if (cou == )//第一次遍历
{
if (count == )
{
sql += " where (SubjectCode =@c";
sql2 += " where (SubjectCode =@c";
tj += " where (SubjectCode =@c";
}
else//性别与年龄输入框都未输入内容
{
sql += " and (SubjectCode =@c";
sql2 += " and (SubjectCode =@c";
tj += " and (SubjectCode =@c";
}
hs.Add("@c", ub.SubjectCode);
cou++;
}
else
{
sql += " or SubjectCode =@d)";
sql2 += " or SubjectCode =@d)";
tj += " or SubjectCode =@d)";
hs.Add("@d", ub.SubjectCode);
}
} }
}
}
}
Tsql方法
5.组合查询 按钮功能赋予
void Button2_Click(object sender, EventArgs e)
{
string sql;//拼接查询前PageCount条数据的语句
string sql2;//查询所有的语句
string tj;
int count;
Tsql(out sql, out sql2,out tj,out count);
Repeater1.DataSource = new StudentData().Select(sql, hs);//数据指向
Repeater1.DataBind();
Label2.Text = "";
Label3.Text = MaxPageNumber(sql2,hs).ToString();//获取当前的最大页
DropDownList2.Items.Clear();
for (int i = ; i <= MaxPageNumber(sql2,hs); i++)//更新快捷跳转列表框
{
DropDownList2.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
}
组合查询
6.分页代码
void btn_next_Click(object sender, EventArgs e)
{
int pagec = Convert.ToInt32(Label2.Text) + ;//获取下一页为第几页
string sql;//拼接查询前PageCount条数据的语句
string sql2;//查询所有的语句
string tj;
int count;
Tsql(out sql, out sql2, out tj, out count);
if (pagec > MaxPageNumber(sql2,hs))//当前为最大页
{
return;
}
else
{
if(count>)//进行的是组合查询的下一页跳转
{
sql += " and Code not in(select top " + (PageCount * (pagec - )) + " Code from Student " + tj + ")";
}
else
{
sql += " where Code not in(select top " + (PageCount * (pagec - )) + " Code from Student " + tj + ")";
}
}
Repeater1.DataSource = new StudentData().Select(sql, hs);//数据指向
Repeater1.DataBind();
Label2.Text = pagec.ToString();//更新当前页面
DropDownList2.SelectedValue = pagec.ToString();
}
下一页
void btn_prev_Click(object sender, EventArgs e)
{
int pagec = Convert.ToInt32(Label2.Text) - ;//获取上一页为第几页
string sql;//拼接查询前PageCount条数据的语句
string sql2;
string tj;
int count;
Tsql(out sql, out sql2, out tj, out count);
if (pagec <= )//当前为第一页
{
return;
}
if (count > )//进行的是组合查询的上一页跳转
{
sql += " and Code not in(select top " + (PageCount * (pagec - )) + " Code from Student " + tj + ")";
}
else
{
sql += " where Code not in(select top " + (PageCount * (pagec - )) + " Code from Student " + tj + ")";
}
List<Student> list = new StudentData().Select(sql, hs);//数据指向
Repeater1.DataSource = list;
Repeater1.DataBind();
Label2.Text = pagec.ToString();//更新当前页面
DropDownList2.SelectedValue = pagec.ToString();
}
上一页
void btn_first_Click(object sender, EventArgs e)
{
string sql;
string sql2;
string tj;
int count;
Tsql(out sql, out sql2, out tj, out count);
List<Student> list = new StudentData().Select(sql, hs);//数据指向
Repeater1.DataSource = list;
Repeater1.DataBind();
Label2.Text = "";
DropDownList2.SelectedValue = "";
}
跳转到第一页
void btn_end_Click(object sender, EventArgs e)
{
string sql;
string sql2;
string tj;
int count;
Tsql(out sql, out sql2, out tj, out count);
if (count > )//进行的是组合查询的末页跳转
{
sql += " and Code not in(select top " + (PageCount * (MaxPageNumber(sql2,hs) - )) + " Code from Student " + tj + ")";
}
else
{
sql += " where Code not in(select top " + (PageCount * (MaxPageNumber(sql2, hs) - )) + " Code from Student " + tj + ")";
}
List<Student> list = new StudentData().Select(sql, hs);//数据指向
Repeater1.DataSource = list;
Repeater1.DataBind();
Label2.Text = MaxPageNumber(sql2,hs).ToString();
DropDownList2.SelectedValue = MaxPageNumber(sql2,hs).ToString();
}
最后一页跳转
void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
string sql;
string sql2;
string tj;
int count;
Tsql(out sql, out sql2, out tj, out count);
if (count > )//进行的是组合查询的快捷跳转
{
sql += " and Code not in(select top " + (PageCount * (Convert.ToInt32(DropDownList2.SelectedValue) - )) + " Code from Student " + tj + ")";
}
else
{
sql += " where Code not in(select top " + (PageCount * (Convert.ToInt32(DropDownList2.SelectedValue) - )) + " Code from Student " + tj + ")";
}
Repeater1.DataSource = new StudentData().Select(sql, hs);//数据指向
Repeater1.DataBind();
Label2.Text = DropDownList2.SelectedValue;
}
快捷跳转
预览图:
Webform(分页与组合查询配合使用)的更多相关文章
- ajax分页与组合查询配合使用
使用纯HTML页与js.ajax.Linq实现分页与组合查询的配合使用 <body> <div id="top"><input type=" ...
- webform 分页、组合查询综合使用
界面: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx ...
- WebForm 分页与组合查询
1.封装实体类 2.写查询方法 //SubjectData类 public List<Subject> Select(string name) { List<Subject> ...
- WebForm 分页、组合查询--2017年1月5日
sql = "select * from Commodity"; hs = new Hashtable(); if (txt_name.Text.Trim() != "& ...
- Webform(Linq高级查、分页、组合查询)
一.linq高级查 1.模糊查(包含) 1 public List<User> Select(string name) 2 { 3 return con.User.Where(r => ...
- Webform(分页、组合查询)
一.分页 1.写查询方法: public List<Student> Select(int PageCount, int PageNumber) {//PageCount为每页显示条数,P ...
- WebForm之Linq组合查询
组合查询 protected void Button1_Click(object sender, EventArgs e) { //默认查询所有,返回的是Table类型,转换成IQueryAble类型 ...
- Linq的分页与组合查询的配合使用
1.首先使用Linq连接数据库,并扩展属性 public partial class User { public string SexStr { get { string end = "&l ...
- linq分页组合查询
一.linq高级查 1.模糊查(字符串包含) 1 public List<User> Select(string name) 2 { 3 return con.User.Where(r = ...
随机推荐
- Java时间日期格式转换
1.这个是系统自动默认的时间格式,或者说是美式格式: Long time = System.currentTimeMillis(); Date date = new Da ...
- 简单的跨平台c/c++日志记录
CLog.h #include <stdlib.h> #pragma once #ifndef _CLOG #define _CLOG #define CLOG_DEBUG 0 #defi ...
- InstallShield Limited Edition for Visual Studio 2013 图文教程(教你如何打包.NET程序)
InstallShield Limited Edition for Visual Studio 2013 图文教程(教你如何打包.NET程序) 标签: InstallShieldVS2013 2015 ...
- 深入理解CSS定位中的偏移
× 目录 [1]定位 [2]包含块 [3]偏移属性[4]绝对定位[5]格式化 [6]auto 前面的话 CSS有三种基本的定位机制:普通流.浮动和绝对定位.利用定位,可以准确地定义元素框相对于其正常位 ...
- Android二维码之创建
由于微信的推出二维码走进了我们的生活,并且越来越多的人们正在发挥着自己的想象力去使用它,来方便我们的生活,我曾经听说过一个笑话,当我们死后,墓碑上不再有墓志铭,而会出现一个记录你一生信息的二维码,当人 ...
- JavaScript获取图片的原始尺寸
页面里的img元素,想要获取它的原始尺寸,以宽度为例可能首先想到的就是width,如下 <img src="http://img11.360buyimg.com/da/g14/M07/ ...
- [OpenCV] Samples 01: drawing
基本的几何图形,标注功能. commondLineParser的使用参见:http://blog.csdn.net/u010305560/article/details/8941365 #includ ...
- MAC下安装与配置MySQL
MAC下安装与配置MySQL MAC下安装与配置MySQL 一 下载MySQL 访问MySQL的官网http://www.mysql.com/downloads/ 然后在页面中会看到“MySQL ...
- 材价看板(1)- 如何建立你的第一个kanban,看看这些暴露的问题你们有没有?
今年负责一个老产品新团队,和几年前的指标组一样,现在的团队没有采用什么研发方法,于是我开始了团队的看板之旅. 12月22日给材价整个部门的产品研发相关人员做了一次kanban工作坊培训, 以及敏 ...
- Windows Azure Active Directory (1) 前言 - 基于声明的验证和授权
<Windows Azure Platform 系列文章目录> 在我们介绍整套系统架构之前,我们需要首先定义一些基本的概念. 用户及其属性: 用户值得是要使用某项服务的个体.用户一般都有一 ...