您的位置:首页 > 其它

Web 上一页下一页 用超链接 用按钮

2015-08-18 10:41 204 查看
using System;
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
{
private MyDBDataContext _Context = new MyDBDataContext();
private const int PAGESIZE = 3;
private int _PageNo = 1; //当前的页号
//获取总页数
public int GetPageCount()
{
//取总行数
int rowsCount = _Context.Car.Count();
//算出总页数
int pageCount = (int)Math.Ceiling(1.0 * rowsCount / PAGESIZE);

return pageCount;
}
public List<Car> GetPagedCar()
{
_PageNo = Convert.ToInt32( PageList.SelectedValue);
var query = _Context.Car.Skip(PAGESIZE * (_PageNo - 1)).Take(PAGESIZE);
return query.ToList();
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillPageList();
ShowCars();
}
}

private void ShowCars()
{
Repeater1.DataSource = GetPagedCar();
Repeater1.DataBind();

//给当前页和一共几页赋值
lblNow.Text = PageList.SelectedValue;
lblAll.Text = PageList.Items.Count.ToString();
}

private void FillPageList()
{
PageList.Items.Clear();

int pageCount = GetPageCount();
for (int i = 1; i <= pageCount; i++)
{
ListItem li = new ListItem(i.ToString(), i.ToString());
PageList.Items.Add(li);
}
}
protected void PageList_SelectedIndexChanged(object sender, EventArgs e)
{
ShowCars();
}
protected void Button1_Click(object sender, EventArgs e)
{
if (PageList.SelectedIndex == 0)
{
return;
}
PageList.SelectedIndex--;
ShowCars();
}
protected void Button2_Click(object sender, EventArgs e)
{
if (PageList.SelectedIndex == GetPageCount() - 1)
{
return;
}

PageList.SelectedIndex++;
ShowCars();
}
}


C#代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: