日期:2014-05-18 浏览次数:20467 次
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Text; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { int startnum = 1; int endnum = 30; int page = 30; int offset = 10; Response.Write(PageNav(startnum,endnum,page, offset)); } /// <summary> /// 分页列表 /// </summary> /// <param name="startnum">页码起始计数</param> /// <param name="endnum">最大页码</param> /// <param name="page">当前页码</param> /// <param name="offset">显示页码范围</param> /// <returns></returns> private string PageNav(int startnum, int endnum, int page, int offset) { if (page < startnum) page = startnum; if (page > endnum) page = endnum; // 取整 int i = page / offset; if ((page % offset) == 0) i--; StringBuilder sb = new StringBuilder(); if (i > 0) sb.Append(startnum.ToString() + "..."); sb.Append(loop(i * offset, (i + 1) * offset)); if (i < ((endnum / offset)-1)) sb.Append("..." + endnum.ToString()); return sb.ToString(); } /// <summary> /// 页码循环 /// </summary> /// <param name="startnum">起始循环数</param> /// <param name="offset">循环次数</param> /// <returns></returns> private string loop(int startnum, int offset) { StringBuilder sb = new StringBuilder(); for (int i = startnum+1; i <= offset; i++) { sb.Append(i.ToString() + " "); } return sb.ToString(); } }