<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default1.aspx.cs" Inherits="Default1" %> <!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> <style type="text/css"> table { 100%; background-color: navy; text-align: center; } #tr_Head { color: white; } .tr_Main { background-color: #e0e0e0; } td { padding: 5px; } </style> </head> <body> <form id="form1" runat="server"> 姓名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 性别:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="查 询" /> <br /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <br /> <br /> <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <table> <tr id="tr_Head"> <td>Ids</td> <td>姓名</td> <td>性别</td> </tr> </HeaderTemplate> <ItemTemplate> <tr class="tr_Main"> <td><%#Eval("Ids") %></td> <td><%#Eval("Name") %></td> <td><%#Eval("Sex") %></td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater><br /> 当前是第【<asp:Label ID="Label2" runat="server" Text="1"></asp:Label>】页, <asp:LinkButton ID="btn_Prev" runat="server">上一页</asp:LinkButton> <asp:LinkButton ID="btn_Next" runat="server">下一页</asp:LinkButton> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default1 : System.Web.UI.Page { int PageCount = 5; protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click;//条件查询 btn_Prev.Click += btn_Prev_Click;//上一页 btn_Next.Click += btn_Next_Click;//下一页 if (IsPostBack == false)//当IsPostBack为空的时候 进行数据填充 { Repeater1.DataSource = new CeShiData().Select(TSQL(1)); Repeater1.DataBind();//数据绑定 } } //下一页 void btn_Next_Click(object sender, EventArgs e) { if (Label2.Text == MaxNumber().ToString())//判断label2的数值是不是最大值 { return;//如果是 就直接跳出 } //1、看看当前第几页,然后将页数+1 int PageNumber = Convert.ToInt32(Label2.Text) + 1;//获取下一页的页数 定义PageNumber获得PageNumber的数值 Label2.Text = PageNumber.ToString();//页面显示页数+1 用Label2来显示 //2、再然后将+1后的页数的数据查出来绑定 Repeater1.DataSource = new CeShiData().Select(TSQL(PageNumber)); Repeater1.DataBind();//重新绑定 Label1.Text = TSQL(PageNumber); } //上一页 void btn_Prev_Click(object sender, EventArgs e) { if (Label2.Text == "1")//判断label2的数值是不是最小值 { return;//是的话直接跳出 } //1、看看当前第几页,然后将页数+1 int PageNumber = Convert.ToInt32(Label2.Text) - 1;//获取下一页的页数 Label2.Text = PageNumber.ToString();//页面显示页数+1 //2、再然后将+1后的页数的数据查出来绑定 Repeater1.DataSource = new CeShiData().Select(TSQL(PageNumber)); Repeater1.DataBind(); Label1.Text = TSQL(PageNumber); } //条件查询 void Button1_Click(object sender, EventArgs e) { //1、将语句拼完 - 调用TSQL()方法 //2、用拼完的语句查数据并绑定 Repeater1.DataSource = new CeShiData().Select(TSQL(1)); Repeater1.DataBind(); Label1.Text = TSQL(1); Label2.Text = "1"; } //核心,如何返回Tsql语句是难点 private string TSQL(int PageNumber) { int cc = 0;//记录一下查询条数 string sql = "select top " + PageCount + " * from CeShi";//查询的语句 string t1 = "";//定义t1 t2 两个值 用来代替sql语句 string t2 = ""; //1、将条件查询的语句拼完 if (TextBox1.Text != "") { sql += " where Name like '%" + TextBox1.Text + "%'"; cc++; t1 = " where Name like '%" + TextBox1.Text + "%'";//确定t1的sql语句 } if (TextBox2.Text != "") { if (cc > 0) { sql += " and Sex like '%" + TextBox2.Text + "%'"; t2 = " and Sex like '%" + TextBox2.Text + "%'";//确定两种状态下t2 的sql语句 } else { sql += " where Sex like '%" + TextBox2.Text + "%'"; t2 = " where Sex like '%" + TextBox2.Text + "%'";//确定两种状态下t2 的sql语句 } cc++; } //2、将分页的语句拼完 if (cc > 0) { sql += " and Ids not in ( select top " + (PageCount * (PageNumber - 1)) + " Ids from CeShi " + t1 + t2 + " )"; } else { sql += " where Ids not in ( select top " + (PageCount * (PageNumber - 1)) + " Ids from CeShi " + t1 + t2 + ")"; } return sql; } //查询全部的复合条件的数据 private string TSQL1() { int cc = 0;//记录一下查询条数 string sql = "select * from CeShi"; //1、将条件查询的语句拼完 if (TextBox1.Text != "")//判断是否为空 { sql += " where Name like '%" + TextBox1.Text + "%'";//模糊查询 cc++; } if (TextBox2.Text != "") { if (cc > 0) { sql += " and Sex like '%" + TextBox2.Text + "%'";//模糊查询 } else { sql += " where Sex like '%" + TextBox2.Text + "%'";//模糊查询 } cc++; } return sql; } //查询最大页数 private int MaxNumber() { List<CeShi> ccc = new CeShiData().Select(TSQL1());//查询数据库中所有的数据 double bbb = ccc.Count / (PageCount * 1.0); //总页数,但是是浮点型 return Convert.ToInt32(Math.Ceiling(bbb));//转化成整数 } //1、做一个功能,先考虑,如何把他们合并起来 //2、如何合并?看看他们有什么共同点 - 都是拼Tsql语句 //3、再一步一步的按照主要功能来做 }