分页组件:
/// <summary> /// 分页组件 /// </summary> public class PagerHelper { /// <summary> /// 分页组件 /// </summary> /// <param name="urlFmt">url格式: newsIndex_{pagenum}.shtml </param> /// <param name="totalsize">总条数</param> /// <param name="pagesize">每页条数</param> /// <param name="currentPage">当前页数</param> /// <returns>分页下标</returns> public static RawString Pager(string urlFmt, long totalsize, long pagesize, long currentPage) { //urlFmt => NewsController.ashx?action=list&pagenum={pagenum} // newsIndex_{pagenum}.shtml long totalPageCount = (long)Math.Ceiling((totalsize * 1.0 / pagesize)); //总页数 long pagebuttom = 4; //8个页标 long firstpage = Math.Max(currentPage - pagebuttom, 1); //第一个页标 long endpage = Math.Min(currentPage + pagebuttom, totalPageCount); //最后一个页标 StringBuilder sb = new StringBuilder(); if(firstpage>1) //如果没有遍历到首页 { string url = urlFmt.Replace("{pagenum}","1"); sb.Append("<li><a href='").Append(url).Append("'>首页</a></li>"); } for (long i = firstpage; i <= endpage; i++) { string url = urlFmt.Replace("{pagenum}", i.ToString()); if (i == currentPage) { sb.Append("<li class='active'><a>第").Append(i).Append("页</a></li>"); } else { sb.Append("<li><a href='").Append(url).Append("'>第").Append(i).Append("页</a></li>"); } } if (endpage < totalPageCount) //如果没有遍历到末页 { string url = urlFmt.Replace("{pagenum}", totalPageCount.ToString()); sb.Append("<li><a href='").Append(url).Append("'>末页</a></li>"); } return new RawString(sb.ToString()); } }
新闻列表 分页 一键静态化:
public void allNewsListStatic(HttpContext context) { #region 新闻列表 分页 一键静态化 AdminHelper.CheckHasPower(context, "新闻一键静态化"); string categoryidStr = context.Request["categoryid"]; int categoryid = VolidHelper.CheckStrToInt(categoryidStr); TD_NEWSCATEGORY newscate = (TD_NEWSCATEGORY)myORM_BLL.SelectModelById(typeof(TD_NEWSCATEGORY), 1, categoryid); long pagesize = 10; //设置每页数量 long totalsize = myORM_BLL.SelectCountByField(typeof(TD_NEWS), 1, "Categoryid=" + categoryid); //总的新闻条数 long totalPageCount = (long)Math.Ceiling(totalsize * 1.0 / pagesize); //总页数 for (long i = 1; i <= totalPageCount; i++) //遍历每一页 { //对于每一页,获得该类别下的新闻集合 List<TD_NEWS> list = new NewsBLL().SelectNewsByCategoryidAndRownum(categoryid, (i - 1) * pagesize + 1, i * pagesize); string cshtml = RazorHelper.RazorParseHtml(context, "~/News/NewsListStatic.cshtml", new { categoryid = categoryid, categoryName = newscate.NAME, newses = list, totalsize = totalsize, pagesize = pagesize, currentpage = i }); //静态化 string pathPre = ConfigurationManager.AppSettings["ViewStaticDirecPre"]; //路径前缀 string fullPath = pathPre + categoryid + "\\newsIndex_" + i + ".shtml"; //全路径 string dir = Path.GetDirectoryName(fullPath); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } File.WriteAllText(fullPath, cshtml); } AdminHelper.RecordOperateLog(context, "新闻列表 一键静态化,其类别是:" + newscate.NAME); context.Response.Redirect("/News/NewsController.ashx?action=list&categoryid=" + categoryid); #endregion }
根据rownum查询的语句:
/// <summary> /// 根据类别ID 按照NO排序 获得指定rownum之间的新闻集合 /// </summary> /// <param name="categoryid">类别ID</param> /// <param name="startnum">rownum起始数</param> /// <param name="endnum">rownum结束数</param> /// <returns>指定rownum之间的新闻集合</returns> public List<TD_NEWS> SelectNewsByCategoryidAndRownum(long categoryid,long startnum ,long endnum) { string sql = @"SELECT * FROM ( SELECT ROW_NUMBER()OVER( order by NO asc) NUM,ID,NO,STATUS,CATEGORYID,CREATEBY,CREATEDATE,LASTEDITBY,LASTEDITDATE,DELETEBY,DELETEDATE,TITLE,CONTENT FROM TD_NEWS WHERE STATUS=1 AND CATEGORYID=:CATEGORYID ) T WHERE T.NUM>:startnum AND T.NUM<:startnum"; DataTable dt= OracleHelper.ExecuteReader(sql, new OracleParameter() { ParameterName = ":CATEGORYID", Value = categoryid }, new OracleParameter() { ParameterName = ":startnum", Value = startnum }, new OracleParameter() { ParameterName = ":startnum", Value = endnum }); List<TD_NEWS> list = new List<TD_NEWS>(); foreach(DataRow row in dt.Rows) { TD_NEWS news = RowToModel(row); list.Add(news); } return list; }