public partial class _Default : 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 = new CarData().SelectAll(num,page);
Repeater1.DataBind();
sumpage.Text = Maxpage().ToString();
bt_first.Enabled = false;
bt_up.Enabled = false;
for (int i = 1; i <= Maxpage(); i++)
{
ListItem li = new ListItem(i.ToString(),i.ToString());
Dr_page.Items.Add(li);
}
}
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 = new CarData().SelectAll(num, a);
Repeater1.DataBind();
//显示当前页数
nowpage.Text = a.ToString();
if (a == 1)
{
bt_first.Enabled = false;
bt_up.Enabled = false;
}
if (a == Maxpage())
{
bt_next.Enabled = false;
bt_last.Enabled = false;
}
}
//首页按钮
void bt_first_Click(object sender, EventArgs e)
{
bt_last.Enabled = true;
bt_next.Enabled = true;
Repeater1.DataSource = new CarData().SelectAll(num, 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;
//执行查询语句
Repeater1.DataSource = new CarData().SelectAll(num, uppage);
Repeater1.DataBind();
//显示当前页数
nowpage.Text = uppage.ToString();
//上一页按钮不可用
if (uppage == 1)
{
bt_first.Enabled = false;
bt_up.Enabled = false;
}
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;
}
//执行查询语句
Repeater1.DataSource = new CarData().SelectAll(num, 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 = new CarData().SelectAll(num, Maxpage());
Repeater1.DataBind();
//显示当前页数
nowpage.Text = Maxpage().ToString();
bt_next.Enabled = false;
}
//总页数方法
public int Maxpage()
{
int end = 0;
int item = new CarData().SelectCount();
decimal a = Convert.ToDecimal(item) / num;
end = Convert.ToInt32(Math.Ceiling(a));
return end;
}
}