SQL分页方法:
////// 显示分页信息 /// /// 总条数 /// 请求的是第几页 /// 每页显示条数 /// 信息类型 ///public static DataSet PageListArticle(out int totleCount, int pageNum, int pageSize,string strType) { try { //sql语句实现分页功能 string sqlstr = "select top " + pageSize + " * from Article" + "where (Article.type=" + strType + " and Article.articleId>(" + "select MAX(articleId) " + " from (select TOP " + pageSize * (pageNum - 1) + " articleId " + "from Article" + "order by articleId) as articleId ))" + "order by Article.articleId"; DataSet result = db.GetTableBySql(sqlstr); //获取总记录数目 totleCount = result.Tables[0].Rows.Count; if (result != null) return result; else return null; } catch (Exception) { totleCount = 0; return null; } }
EF分页方法:
public IListGetTestPageLoad(int pagesize, int pageindex, out int total) { try { TestOnLineEntities db = new TestOnLineEntities(); var testlist = from vtest in db.V_Test orderby vtest.TestTime descending select vtest; var result = testlist.ToList(); total = ViewTest.Count; //跳过的总条数 int totalNum = (pageindex - 1) * pagesize; ViewTest = ViewTest.Skip(totalNum).ToList(); if (ViewTest.Count > pagesize) { ViewTest.RemoveRange(pagesize, total - pagesize); } return ViewTest; } catch (Exception) { total = 0; return null; } }
新闻分页:
思路:在新闻编辑时,在需要分页的地方插入分页符(如“【分页符】”),显示时,根据分页符的位置进行分页。
using System.Text;//为内容生成分页 public static string ContentPage(string content, int page) { string[] array_content = content.Split(new string[] { "【分页符】" }, StringSplitOptions.None); int length = array_content.Length; if (length <= 1) { return content; } return array_content[page - 1] + MakePage(length, page); } public static string MakePage(int length, int page) { string url; int ic = HttpContext.Current.Request.RawUrl.IndexOf("&page"); if (ic > 0) { url = HttpContext.Current.Request.RawUrl.Remove(ic); } else { url = HttpContext.Current.Request.RawUrl; } StringBuilder builder = new StringBuilder(); builder.Append(""); return builder.ToString(); } "); builder.Append("