/// <summary> /// Linq分页 /// </summary> int pagesize = 2;//每页条数 int recordcount = 0;//总条数 int pageindex = 1;//当前第几页 public static string con = ConfigurationManager.ConnectionStrings["MySQLDBConnectionString"].ConnectionString; GuestBookDataContext ctx = new GuestBookDataContext(con); public StringBuilder sb = new StringBuilder(); public void GetPage() { if (!String.IsNullOrEmpty(Request.QueryString["page"])) { pageindex = Convert.ToInt32(Request.QueryString["page"]); } recordcount = ctx.tbGuestBook.Count(); int pagecount = (int)Math.Ceiling((decimal)recordcount/pagesize);//总页数 sb.Append("共<font style=‘color:#FF0000‘>"+pageindex+"</font>/"+pagecount+"页 "); for (int i = 1; i <= pagecount; i++) { sb.AppendFormat(@"<a href=‘?page={0}‘>{0}<a/>",i); } var data = (from gb in ctx.tbGuestBook orderby gb.PostTime descending select gb).Skip((pageindex-1)*pagesize).Take(pagesize); rpt_Message.DataSource = data; rpt_Message.DataBind(); }
aspx调用页码代码:
<%=sb.ToString() %>
时间: 2024-10-23 18:15:47