前台代码:
<%@ 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>
<style>
* {
padding: 0px;
margin: 0px;
} #div0 {
width: 1000px;
position: relative;
margin: 0px auto;
} #tb1 {
width: %;
text-align: center;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div id="div0">
Name:<asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
Brand:<asp:TextBox ID="txt_brand" runat="server"></asp:TextBox>
Price:<asp:DropDownList ID="ddl_price" runat="server">
<asp:ListItem Value="=">等于</asp:ListItem>
<asp:ListItem Value=">=">大于等于</asp:ListItem>
<asp:ListItem Value="<=">小于等于</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txt_price" runat="server"></asp:TextBox>
<asp:Button ID="btn_selectsome" runat="server" Text="查询" />
<asp:Button ID="btn_selectall" runat="server" Text="查询全部" />
<br />
<br />
<table id="tb1" border="" cellpadding="" cellspacing="">
<tr style="background-color: navy; color: white;">
<td>Code</td>
<td>Namge</td>
<td>Brand</td>
<td>Time</td>
<td>Oil</td>
<td>Powers</td>
<td>Exhaust</td>
<td>Price</td>
<td>Pic</td>
<td>操作</td>
</tr>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<tr>
<td><%#Eval("Code") %></td>
<td><%#Eval("Name") %></td>
<td><%#Eval("Brand") %></td>
<td><%#Eval("Time") %></td>
<td><%#Eval("Oil") %></td>
<td><%#Eval("Powers") %></td>
<td><%#Eval("Exhaust") %></td>
<td><%#Eval("Price") %></td>
<td><%#Eval("Pic") %></td>
<td>
<a href="Update.aspx?name=<%#Eval("name") %>">修改</a>
<a href="Delete.aspx?name=<%#Eval("name") %>">删除</a>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<br />
共【<asp:Label ID="lb_AllInf" runat="server" Text="加载中..."></asp:Label>】条数据
当前第【<asp:Label ID="lb_NowPage" runat="server" Text="加载中..."></asp:Label>】页
共【<asp:Label ID="lb_MaxPageNum" runat="server" Text="加载中..."></asp:Label>】页
<asp:Button ID="btn_prev" runat="server" Text="上一页" />
<asp:Button ID="btn_next" runat="server" Text="下一页" /> </div>
</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 _Default : System.Web.UI.Page
{
//全局变量
//▲▲▲②每页显示的数据条数▲▲▲
int vPagePerCount = ; protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//首次加载全部数据
Select();
lb_NowPage.Text = "";
} //<查询全部>按钮
btn_selectall.Click += btn_selectall_Click;
//<查询>按钮
btn_selectsome.Click += btn_selectsome_Click;
//<下一页>
btn_next.Click += btn_next_Click;
//<上一页>
btn_prev.Click += btn_prev_Click; }
//<查询全部>按钮
void btn_selectall_Click(object sender, EventArgs e)
{
//先清空文本框中的内容
txt_name.Text = "";
txt_brand.Text = "";
ddl_price.SelectedValue = "=";
txt_price.Text = "";
//再查询绑定数据,并跳至第一页
Select();
}
//<查询>按钮
void btn_selectsome_Click(object sender, EventArgs e)
{
//查询绑定数据,并跳至第一页
Select();
}
//<下一页>
void btn_next_Click(object sender, EventArgs e)
{
//获取当前页码和最大页码
int NowPageNum = Convert.ToInt32(lb_NowPage.Text);
int MaxPageNum = Convert.ToInt32(lb_MaxPageNum.Text);
//比较下一页页码和最大页码,只有当下一页页码不大于最大页码时才跳至下一页
if (NowPageNum + <= MaxPageNum)
{
Select(NowPageNum + );
}
}
//<上一页>
void btn_prev_Click(object sender, EventArgs e)
{
//获取当前页码
int NowPageNum = Convert.ToInt32(lb_NowPage.Text);
//比较上一页页码和第一页,只有当上一页页码不小于第一页时才跳至上一页
if (NowPageNum - >= )
{
Select(NowPageNum - );
}
} //====================封装方法=========================
//查询数据并绑定方法 参数:要跳至的页数 返回:无返回值
public void Select(int PageNum)
{
//获取查询条件
string name = txt_name.Text.Trim();
string brand = txt_brand.Text.Trim();
string price = txt_price.Text.Trim();
//查询并绑定数据
using (mydbDataContext con = new mydbDataContext())
{
//查询
IQueryable<Car> clist = con.Car.AsQueryable();
if (name.Length > )
clist = clist.Where(r => r.Name.Contains(name));
if (brand.Length > )
clist = clist.Where(r => r.Brand.Contains(brand));
if (price.Length > )
{
string f = ddl_price.SelectedValue;
if (f == "=")
clist = clist.Where(r => r.Price == Convert.ToDecimal(price));
if (f == ">=")
clist = clist.Where(r => r.Price >= Convert.ToDecimal(price));
if (f == "<=")
clist = clist.Where(r => r.Price <= Convert.ToDecimal(price)); }
//绑定数据
Repeater1.DataSource = clist.Skip((PageNum - ) * vPagePerCount).Take(vPagePerCount);
Repeater1.DataBind();
////修改Label显示
//总数据的条数
decimal AllListCount = clist.Count();
lb_AllInf.Text = AllListCount.ToString();
//最大页数
int MaxPageNum = Convert.ToInt32(Math.Ceiling(AllListCount / vPagePerCount));
lb_MaxPageNum.Text = MaxPageNum.ToString();
//当前页码
lb_NowPage.Text = PageNum.ToString(); }
} }