这个文件我会打包整理,发布的
前端代码:
后台代码:
public int PageCount { get; set; } public int PageIndex { get; set; } public int RecordCount { get; set; } GuestBookDAL gb = new GuestBookDAL();//这里是自己的DAL protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindRep(); } } #region 分页获取数据 ////// 分页获取数据 /// private void BindRep() { int pageIndex ; if (!int.TryParse(Request.QueryString["pageIndex"],out pageIndex))//如果能转化成整数,就得到,否则为1 { pageIndex=1; } int pageSize = 5;//每页显示的记录数 int pageCount = GetPageCount(pageSize);//获取总页数 PageCount = pageCount; //确定pageIndex的取值范围 pageIndex=pageIndex<1?1:pageIndex; pageIndex = pageIndex > pageCount ? pageCount : pageIndex; PageIndex = pageIndex; int start = (pageIndex - 1) * pageSize + 1; int end = pageIndex * pageSize; rep.DataSource = gb.GetPageEntityList(start, end);//获取分页数据 rep.DataBind(); } #endregion #region 计算总的页数 ////// 计算总的页数 /// /// 每页显示记录数 ///public int GetPageCount(int pageSize) { int recordCount = gb.GetRecordCount();//得到总的记录数 RecordCount = recordCount; int pageCount = Convert.ToInt32(Math.Ceiling((double)recordCount / pageSize));//计算得到总的页数。用到了天花板函数 return pageCount; } #endregion protected void btn_Click(object sender, EventArgs e)//跳转按钮点击事件 { int txtnum; //txtnum = Convert.ToInt32(num.Text.Trim()); if (num.Text.Trim()==null||!int.TryParse(num.Text.Trim(),out txtnum)) { Response.Write(""); } else { Response.Redirect("Default.aspx?pageIndex="+txtnum); } } }
DAL.cs(类文件)
SqlHelper sqlhelper = new SqlHelper();----这个在上一个博客中写到过,可以自己去看下 Guestbook gb = new Guestbook(); #region 分页列表显示 ////// 分页列表显示 /// /// 开始页码 /// 结束页码 ///public DataTable GetPageEntityList(int start, int end) { DataTable dt = new DataTable(); string sql = "select *from (select ROW_NUMBER() over(order by id)as num,*from gb_data) as t where t.num>=@start and t.num<=@end"; SqlParameter[] pars = new SqlParameter[]{ new SqlParameter("@start",start), new SqlParameter("@end",end) }; dt = sqlhelper.ExecuteQuery(sql, pars,CommandType.Text); return dt; } #endregion #region 获取总的记录数 /// /// 获取总的记录数 /// ///public int GetRecordCount() { string sql = "select count(*)from gb_data"; string res = sqlhelper.ExecuteScalar(sql, CommandType.Text); return Convert.ToInt32(res); } #endregion
pagebar.cs(这个类文件是做的数字分页用的,直接使用就可以了)对应前端代码
////// 获取数字页码条 /// /// 当前页码值 /// 总的页数 ///public static string GetPageBar(int pageIndex, int pageCount) { if (pageCount==1) { return string.Empty;//只有一页不用页码条 } //计算起始位置和终止位置 int start = pageIndex - 5; if (start < 1) { start = 1; } int end = start + 9;//终止位置 if (end>pageCount) { end = pageCount; start = end - 9 > 1 ? end - 9 : 1; } StringBuilder sb = new StringBuilder(); sb.Append(string.Format("首页")); if (pageIndex>1) { sb.Append(string.Format("上一页",pageIndex-1)); } for (int i = start; i <= end; i++) { if (i==pageIndex)//如果循环的数字与当前页码相等,就不需要加超链接 { sb.Append(i); } else { sb.Append(string.Format("{0}", i));//不写页面名字,方便别的页面调用 } } if (pageIndex 下一页", pageIndex + 1)); } sb.Append(string.Format(" 尾页",pageCount)); return sb.ToString(); }
附录:CSS
.page_nav { TEXT-ALIGN: center; PADDING-BOTTOM: 15px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; FONT: 12px/24px Arial; COLOR: #666; CLEAR: both; PADDING-TOP: 15px;}.page_nav A { BORDER-BOTTOM: #dbe5ee 1px solid; BORDER-LEFT: #dbe5ee 1px solid; PADDING-BOTTOM: 0px; MARGIN: 0px 2px; PADDING-LEFT: 8px; PADDING-RIGHT: 8px; DISPLAY: inline-block; FONT: 12px/22px Arial, Helvetica, sans-serif; BACKGROUND: #fff; HEIGHT: 22px; COLOR: #333; BORDER-TOP: #dbe5ee 1px solid; CURSOR: pointer; BORDER-RIGHT: #dbe5ee 1px solid; PADDING-TOP: 0px; border-radius: 2px; -moz-border-radius: 2px; -webkit-border-radius: 2px; -khtml-border-radius: 2px}.page_nav STRONG { BORDER-BOTTOM: medium none; BORDER-LEFT: medium none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 24px; MARGIN: 0px 3px; PADDING-LEFT: 8px; PADDING-RIGHT: 8px; DISPLAY: inline-block; BACKGROUND: #07519a; HEIGHT: 24px; COLOR: #fff; BORDER-TOP: medium none; FONT-WEIGHT: normal; BORDER-RIGHT: medium none; TEXT-DECORATION: none; PADDING-TOP: 0px; border-radius: 2px; -moz-border-radius: 2px; -webkit-border-radius: 2px; -khtml-border-radius: 2px}.page_nav A:hover { BORDER-BOTTOM: medium none; BORDER-LEFT: medium none; LINE-HEIGHT: 24px; MARGIN: 0px 3px; BACKGROUND: #07519a; HEIGHT: 24px; COLOR: #fff; BORDER-TOP: medium none; BORDER-RIGHT: medium none; TEXT-DECORATION: none}.page_nav A.select { CURSOR: default}