多条件查询与上下翻页
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
{
protected void Page_Load(object sender, EventArgs e)
{ //绑定显示数据
if (!IsPostBack)
{
TestDataContext context = new TestDataContext();
Repeater1.DataSource = context.Car;
Repeater1.DataBind();
}
}
protected void btnSelect_Click(object sender, EventArgs e)
{ // 双击查询按钮
TestDataContext context = new TestDataContext();
List<Car> list = context.Car.ToList();
//判断第一个条件
string key = txtName.Text;
if (key != "")
{
list = list.Where(p=>p.Name.Contains(key)).ToList();
//使关键字变色
foreach (Car data in list)
{
string s = data.Name.Replace(key, "<mark>" + key + "</mark>");
data.Name = s;
}
}
//判断第二个条件
string mi = txtJGmin.Text;
string ma = txtJGmax.Text;
if (mi != "" && ma != "")
{
decimal min = Convert.ToDecimal(txtJGmin.Text);
decimal max = Convert.ToDecimal(txtJGmax.Text);
list = list.Where(p=>p.Price.Value>=min && p.Price.Value<=max).ToList();
}
//指定数据源
Repeater1.DataSource = list;
Repeater1.DataBind();
}
上下翻页:
private int zys = 0;
private int n = 1;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["n"] = 1;
showye(n);
}
}
public string showdatetime()
{
return Convert.ToDateTime(Eval("Time")).ToString("yyyy年MM余额dd日");
}
public void filltxt()
{
DataClassesDataContext context = new DataClassesDataContext();
int a = context.Car.Count();
if (a % 5 == 0)
{
zys = a / 5;
}
else
{
zys = a / 5 + 1;
}
}
protected void Button1_Click(object sender, EventArgs e)
{ //上翻页
if (n <= 1)
{
Literal1.Text = "<script type='text/javascript'>alert('已经是第一页了')<script>";
}
else
{
Session["n"] = n - 1;
showye(n - 1);
}
}
public void showye(int x)
{
DataClassesDataContext context = new DataClassesDataContext();
Repeater1.DataSource = context.Car.Skip((x - 1) * 5).Take(5);
Repeater1.DataBind(); Label1.Text = "当前第:" + x + "页";
}
protected void Button2_Click(object sender, EventArgs e) //往下翻页
{
if (n >= zys) //判断是否大于总页数
{
Literal1.Text = "<script type='text/javascript'>alert('已经是最后一页了')</script>";
}
else
{
Session["n"] = n + 1;
showye(n + 1);
Literal1.Text = "";
}
}
protected void Button3_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(TextBox1.Text);
if (a > 1 && a <= zys)
{
Session["n"] = a;
showye(a);
Literal1.Text = "";
}
else
{
Literal1.Text="<script type='text/javascript'>alert('页数超出范围')</script>";
}
filltxt();
if (Session["n"] != null)
{
n = Convert.ToInt32(Session["n"]);
}
}
}