PageBarHelper
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CZBK.ItcastProject.Common { public class PageBarHelper { public static string GetPageaBar(int pageIndex, int pageCount) { if (pageCount==1) { return string.Empty; } int star = pageIndex - 5;//计算起始位置,要求页面上显示10个数字页码。 if (star<1) { star = 1; } int end = star + 9;//计算终止位置 if (end>pageCount) { end = pageCount; //重新计算一下star的值 star = end - 9 < 1 ? 1 : end - 9; } StringBuilder sb = new StringBuilder(); if (pageIndex > 1) { sb.AppendFormat("<a href=‘NewList.aspx?pageIndex={0}‘>上一页</a>", pageIndex - 1); } for (int i = star; i <= end; i++) { if (i==pageIndex) { sb.Append(i); } else { sb.AppendFormat("<a href=‘NewList.aspx?pageIndex={0}‘>{0}</a>",i); } } if (pageIndex < pageCount) { sb.AppendFormat("<a href=‘NewList.aspx?pageIndex={0}‘>下一页</a>", pageIndex + 1); } return sb.ToString(); } } }
页面引入样式pageBarStyle.css
.page_nav{clear:both; padding:15px 0; color:#666; font:normal 12px/24px Arial; text-align:center;} .page_nav a{display:inline-block; height:22px; margin:0 2px; padding:0 8px; text-decoration:none; border:solid 1px #dbe5ee; -moz-border-radius:2px; -webkit-border-radius:2px; -khtml-border-radius:2px; border-radius:2px; background:#fff; color:#333; font:normal 12px/22px Arial, Helvetica, sans-serif; cursor:pointer;} .page_nav strong{display:inline-block; height:24px; margin:0 3px; padding:0 8px; border:none; -moz-border-radius:2px; -webkit-border-radius:2px; -khtml-border-radius:2px; border-radius:2px; background:#C00; color:#fff; font-weight:normal; line-height:24px; text-decoration:none;} .page_nav a:hover, .page_nav a.on{height:24px; margin:0 3px; border:none; background:#C00; color:#fff; line-height:24px; text-decoration:none;} .page_nav a.select{cursor:default;} .page_nav .view_all{display:block; text-align:center;} .page_nav .view_all a{height:auto; margin:0; padding:0; border:none; color:#06c; line-height:24px;} .page_nav .view_all a:hover{height:auto; margin:0; padding:0; background:none;}
后台代码
using CZBK.ItcastProject.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace CZBK.ItcastProject.WebApp._2015_5_30 { public partial class NewList : System.Web.UI.Page { public string StrHtnl { get; set; } public int pageIndex { get; set; } public int pageCount { get; set; } protected void Page_Load(object sender, EventArgs e) { int pageSize = 10; pageIndex = Convert.ToInt32(Request.QueryString["pageIndex"]) == 0 ? 1 : Convert.ToInt32(Request.QueryString["pageindex"]); BLL.UserInfoService userinfoservice = new BLL.UserInfoService(); pageCount = userinfoservice.GetPageCount(pageSize);//获取总页数 pageIndex = pageIndex < 1 ? 1 : pageIndex; pageIndex = pageIndex > pageCount ? pageCount : pageIndex; List<UserInfo> list = userinfoservice.GetPageList(pageIndex, pageSize);//分页数据 StringBuilder sb = new StringBuilder(); foreach (UserInfo userinfo in list) { if (userinfo!=null) { sb.AppendFormat("<li><span>{0}</span><a href=‘#‘>{1}</a></li>",userinfo.RegTime.ToShortDateString(),userinfo.UserName); } } StrHtnl = sb.ToString(); } } }
DAL层代码
/// <summary> /// 根据指定的范围,获取指定的数据 /// </summary> /// <param name="start"></param> /// <param name="end"></param> /// <returns></returns> public List<UserInfo> GetPageList(int start,int end) { string sql = "SELECT * FROM (SELECT *,ROW_NUMBER() OVER (ORDER BY id) AS num FROM dbo.UserInfo) AS t WHERE t.num>= @start and num <[email protected]"; SqlParameter[] pars ={ new SqlParameter("@start",SqlDbType.Int,4), new SqlParameter("@end",SqlDbType.Int,4) }; pars[0].Value = start; pars[1].Value = end; DataTable da = new DataTable(); da=SqlHelper.GetDataTable(sql, CommandType.Text, pars); List<UserInfo> list = null; if (da.Rows.Count > 0) { list = new List<UserInfo>(); UserInfo userInfo = null; foreach (DataRow row in da.Rows) { userInfo = new UserInfo(); LoadEntity(userInfo, row); list.Add(userInfo); } } return list; } /// <summary> /// 获取总的记录数 /// </summary> /// <returns></returns> public int GetRecordCount() { string sql = "select count(*) from UserInfo"; return Convert.ToInt32(SqlHelper.ExecuteScalar(sql, CommandType.Text)); }
BLL层代码
/// <summary> /// 计算获取数据的访问,完成分页 /// </summary> /// <param name="pageIndex">当前页码</param> /// <param name="pageSize">每页显示的记录数据</param> /// <returns></returns> public List<UserInfo> GetPageList(int pageIndex, int pageSize) { int start = (pageIndex - 1) * pageSize + 1; int end = pageIndex * pageSize; return UserInfoDal.GetPageList(start, end); } /// <summary> /// 获取总的页数 /// </summary> /// <param name="pageSize">每页显示的记录数</param> /// <returns></returns> public int GetPageCount(int pageSize) { int recoredCount = UserInfoDal.GetRecordCount();//获取总的记录数. int pageCount = Convert.ToInt32(Math.Ceiling((double)recoredCount / pageSize)); return pageCount; }
Model层
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CZBK.ItcastProject.Model { public class UserInfo { public int Id { get; set; } public string UserName { get; set; } public string UserPass { get; set; } public DateTime RegTime { get; set; } public string Email { get; set; } } }
aspx页面部分,两种不同分页方式
<div class="pages"><a href="NewList.aspx?pageIndex=1">首页</a> | <a href="NewList.aspx?pageIndex=<%=pageIndex-1<1?1:pageIndex-1 %>">前页</a> | <a href="NewList.aspx?pageIndex=<%=pageIndex+1>pageCount?pageCount:pageIndex+1 %>">后页</a> | <a href="NewList.aspx?pageIndex=<%=pageCount %>">尾页</a> 页次:<%=pageIndex %>/<%=pageCount %>页 </div> <hr /> <div class="page_nav"> <%=PageBarHelper.GetPageaBar(pageIndex,pageCount) %> </div>
时间: 2024-09-30 10:50:23