標籤:
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()); } } }
asp.net 使用querystring實現資料庫多條資料的分頁