1、在page_load中插入以下代码。
protected void Page_Load(object sender, EventArgs e) { if (Request.Params["page"] != null && Request.Params["page"].Trim() != "") { page = (Convert.ToInt32(Request.Params["page"])); maxpage = Databind(page, maxnum); lblMaxnum.Text = maxpage.ToString(); } else { page = 1; maxpage = Databind(page, maxnum); lblMaxnum.Text = maxpage.ToString(); } }
2、Databind方法代码如下。pagenum表示第几页,maxnum表示每页数据最多几条。
public int Databind(int pagenum,int maxnum) { zygzsDataClassesDataContext context = new zygzsDataClassesDataContext(); var count = context.T_Paper.Count(); int maxpage = (count / maxnum) + 1; if (maxpage >= 2) { var res = context.T_Paper.Skip((pagenum - 1) * maxnum).Take(maxnum); rptList.DataSource = res; rptList.DataBind(); } else { var res = context.T_Paper.Take(maxnum); rptList.DataSource = res; rptList.DataBind(); } return maxpage; }
3、下一页按钮,代码如下。
protected void lbtnNext_Click(object sender, EventArgs e) { if (page < maxpage) { if (Request.Params["page"] != null && Request.Params["page"].Trim() != "") { page = (Convert.ToInt32(Request.Params["page"])) + 1; Response.Redirect("Paper.aspx?page=" + page.ToString()); } else { page = 2; Response.Redirect("Paper.aspx?page=" + page.ToString()); } } }
时间: 2024-10-11 03:26:32