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 Default2 : System.Web.UI.Page
{
int num = 5;
protected void Page_Load(object sender, EventArgs e)
{
int page = Convert.ToInt32(nowpage.Text);
if (!IsPostBack)
{
Repeater1.DataSource = End(1);
Repeater1.DataBind();
sumpage.Text = Maxpage().ToString();
if (page == Maxpage())
{
bt_first.Enabled = false;
bt_up.Enabled = false;
bt_next.Enabled = false;
bt_last.Enabled = false;
}
for (int i = 1; i <= Maxpage(); i++)
{
ListItem li = new ListItem(i.ToString(), i.ToString());
Dr_page.Items.Add(li);
}
}
Button1.Click += Button1_Click;
bt_last.Click += bt_last_Click;
bt_next.Click += bt_next_Click;
bt_up.Click += bt_up_Click;
bt_first.Click += bt_first_Click;
bt_go.Click += bt_go_Click;
}
void bt_go_Click(object sender, EventArgs e)
{
bt_first.Enabled = true;
bt_up.Enabled = true;
bt_next.Enabled = true;
bt_last.Enabled = true;
int a = Convert.ToInt32(Dr_page.SelectedValue);
Repeater1.DataSource = End(a);
Repeater1.DataBind();
//显示当前页数
nowpage.Text = a.ToString();
if (a == 1)
{
bt_first.Enabled = false;
bt_up.Enabled = false;
return;
}
if (a == Maxpage())
{
bt_next.Enabled = false;
bt_last.Enabled = false;
return;
}
}
void bt_first_Click(object sender, EventArgs e)
{
bt_last.Enabled = true;
bt_next.Enabled = true;
Repeater1.DataSource = End(1);
Repeater1.DataBind();
//显示当前页数
nowpage.Text = "1";
bt_up.Enabled = false;
}
void bt_up_Click(object sender, EventArgs e)
{
//获取下一页查询页数
int page = Convert.ToInt32(nowpage.Text);
int uppage = page - 1;
if (uppage < 1)
{
return;
}
if (page == 1)
{
bt_first.Enabled = false;
bt_up.Enabled = false;
}
//执行查询语句
Repeater1.DataSource = End(uppage);
Repeater1.DataBind();
//显示当前页数
nowpage.Text = uppage.ToString();
//上一页按钮不可用
bt_next.Enabled = true;
bt_last.Enabled = true;
}
void bt_next_Click(object sender, EventArgs e)
{
//上一页按钮可用
bt_first.Enabled = true;
bt_up.Enabled = true;
//获取下一页查询页数
int page = Convert.ToInt32(nowpage.Text);
int nextpage = page + 1;
//下一页按钮不可用
if (nextpage > Maxpage())
{
bt_last.Enabled = false;
bt_next.Enabled = false;
return;
}
//执行查询语句
Repeater1.DataSource = End(nextpage);
Repeater1.DataBind();
//显示当前页数
nowpage.Text = nextpage.ToString();
}
void bt_last_Click(object sender, EventArgs e)
{
bt_first.Enabled = true;
bt_up.Enabled = true;
Repeater1.DataSource = End(Maxpage());
Repeater1.DataBind();
//显示当前页数
nowpage.Text = Maxpage().ToString();
bt_next.Enabled = false;
}
//总页数方法
public int Maxpage()
{
int end = 0;
int item = Count();
decimal a = Convert.ToDecimal(item) / num;
end = Convert.ToInt32(Math.Ceiling(a));
return end;
}
void Button1_Click(object sender, EventArgs e)
{
int count = 0;
int page = Convert.ToInt32(nowpage.Text);
string tsql = "select top " + num + " *from Car where ids not in(select top " + num * (page - 1) + " ids from Car ";
string tasp = " ";
Hashtable hs = new Hashtable();
if (name.Text.Length > 0)
{
tsql += " where name like @a";
tasp += " )and name like @a";
hs.Add("@a", "%" + name.Text.Trim() + "%");
count++;
}
if (oiltext.Text.Length > 0)
{
if (count > 0)
{
tsql += " and oil " + oil.SelectedValue + "@b";
tasp += " and oil" + oil.SelectedValue + "@b";
}
else
{
tsql += " where oil " + oil.SelectedValue + "@b";
tasp += " )and oil" + oil.SelectedValue + "@b";
}
hs.Add("@b", oiltext.Text.Trim());
count++;
}
if (price.SelectedValue != null)
{
if (count > 0)
{
tsql += " and " + price.SelectedValue;
tasp += " and " + price.SelectedValue;
}
else
{
tsql += " where " + price.SelectedValue;
tasp += " )and " + price.SelectedValue;
}
count++;
}
else
{
tsql += ")";
}
tsql = tsql + tasp;
Label1.Text = tsql;
Repeater1.DataSource = new CarData().SelectAll(tsql, hs);
Repeater1.DataBind();
}
//总查询方法
public List<Car> End(int page)
{
int count = 0;
string tsql = "select top " + num + " *from Car where ids not in(select top " + num * (page - 1) + " ids from Car ";
string tasp = " ";
Hashtable hs = new Hashtable();
if (name.Text.Length > 0)
{
tsql += " where name like @a";
tasp += " )and name like @a";
hs.Add("@a", "%" + name.Text.Trim() + "%");
count++;
}
if (oiltext.Text.Length > 0)
{
if (count > 0)
{
tsql += " and oil " + oil.SelectedValue + "@b";
tasp += " and oil" + oil.SelectedValue + "@b";
}
else
{
tsql += " where oil " + oil.SelectedValue + "@b";
tasp += " )and oil" + oil.SelectedValue + "@b";
}
hs.Add("@b", oiltext.Text.Trim());
count++;
}
if (price.SelectedValue != null)
{
if (count > 0)
{
tsql += " and " + price.SelectedValue;
tasp += " and " + price.SelectedValue;
}
else
{
tsql += " where " + price.SelectedValue;
tasp += " )and " + price.SelectedValue;
}
count++;
}
else
{
tsql += ")";
}
tsql = tsql + tasp;
Label1.Text = tsql;
List<Car> clist = new CarData().SelectAll(tsql, hs);
return clist;
}
//查询最大页数
public int Count()
{
int end = 0;
int count = 0;
string tsql = "select count(*) from Car ";
Hashtable hs = new Hashtable();
if (name.Text.Length > 0)
{
tsql += " where name like @a";
hs.Add("@a", "%" + name.Text.Trim() + "%");
count++;
}
if (oiltext.Text.Length > 0)
{
if (count > 0)
{
tsql += " and oil " + oil.SelectedValue + "@b";
}
else
{
tsql += " where oil " + oil.SelectedValue + "@b";
}
hs.Add("@b", oiltext.Text.Trim());
count++;
}
if (price.SelectedValue != null)
{
if (count > 0)
{
tsql += " and " + price.SelectedValue;
}
else
{
tsql += " where " + price.SelectedValue;
}
count++;
}
end = new CarData().SelectCount(tsql, hs);
return end;
}
}