public partial class _Default : System.Web.UI.Page
{
private mydbDataContext _context = new mydbDataContext();//建立连接
private const int PAGESIZE = ;//定义每页大小为三个 //取得分页信息************重要***********
public List<Car> GetPageCar(int pageNo)
{
var query = _context.Car.Skip(PAGESIZE * (pageNo - )).Take(PAGESIZE);
return query.ToList();
} //获取总页数************重要***********
public int GetPageCount()
{
int rowsCount = _context.Car.Count();//获得总行数
int pageCount = (int)Math.Ceiling(1.0 * rowsCount / PAGESIZE);
return pageCount;
} protected void Page_Load(object sender, EventArgs e)
{
int nowPage =;//初始页为1
if (Request["pageno"]!=null)//第一次打开pageno为空,当不为空的时候就给nowpage赋值
{
nowPage = Convert.ToInt32(Request["pageno"]);
}
//显示列表中的信息
ShowPageCars(nowPage); //显示上一页下一页的数据,首页和尾页
ShowPrevNextLink(nowPage); } //显示上一页下一页首页尾页信息
private void ShowPrevNextLink(int nowPage)
{
//给上一页,下一页数据
int pageCount = GetPageCount();
//控制下一页连接
if (nowPage == pageCount)
{
linkNext.Enabled = false;
linkLast.Enabled = false;
}
else
{
linkNext.Enabled = true;
linkLast.Enabled = true;
linkNext.NavigateUrl = "Default.aspx?pageno=" + (nowPage + ).ToString();
//控制尾页
linkLast.NavigateUrl = "Default.aspx?pageno=" + pageCount;
}
//控制上一页连接
if (nowPage == )
{
linkPrev.Enabled = false;
linkFrist.Enabled = false;
}
else
{
linkPrev.Enabled = true;
linkFrist.Enabled = true;
linkPrev.NavigateUrl = "Default.aspx?pageno=" + (nowPage - ).ToString();
//控制首页连接
linkFrist.NavigateUrl = "Default.aspx?pageno=1";
}
} //显示列表信息
private void ShowPageCars(int nowPage)
{
List<Car> list = GetPageCar(nowPage);
//给Repeater数据
Repeater1.DataSource = list;
Repeater1.DataBind();
} //跳转
protected void btnGo_Click(object sender, EventArgs e)
{
if (txtPageNO.Text == "")
{ }
else
{
int goNo = Convert.ToInt32(txtPageNO.Text);
if (goNo < )
{
Response.Redirect("Default.aspx");
}
else if (goNo > GetPageCount())
{
Response.Redirect("Default.aspx?pageno=" + GetPageCount());
}
else
{
Response.Redirect("Default.aspx?pageno=" + goNo);
}
}
}
}
效果图如下
首页(name): linkFirst
上一页(name):linkPrev
下一页(name):,linkNext
尾页(name):linkLast
以上都是HyperLink控件(超链接)
文本框(name):txtPageNo
跳转按钮(name):btnGo
分页问题,每次点击页面都将从新加载.可考虑Ajax优化