简单的分页
这个文件我会打包整理,发布的
前端代码:
<form runat="server"> <div class="page_nav"> <span style="color: black">共<%=RecordCount %>条数据</span> <span style="color: black">当前第<%=PageIndex %>页 /共<%=PageCount %> 页</span> <%=WebPaging.PageBar.GetPageBar(PageIndex,PageCount)%> <span> 跳转到<asp:TextBox ID="num" runat="server" Width="30px"></asp:TextBox><asp:Button ID="btn" runat="server" Text="跳转" Width="60px" OnClick="btn_Click" /></span> </div> </form>
后台代码:
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 分页获取数据 /// <summary> /// 分页获取数据 /// </summary> 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 计算总的页数 /// <summary> /// 计算总的页数 /// </summary> /// <param name="pageSize">每页显示记录数</param> /// <returns></returns> 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("<script>alert('请输入数字!');</script>"); } else { Response.Redirect("Default.aspx?pageIndex="+txtnum); } } }
DAL.cs(类文件)
SqlHelper sqlhelper = new SqlHelper();----这个在上一个博客中写到过,可以自己去看下 Guestbook gb = new Guestbook(); #region 分页列表显示 /// <summary> /// 分页列表显示 /// </summary> /// <param name="start">开始页码</param> /// <param name="end">结束页码</param> /// <returns></returns> 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 获取总的记录数 /// <summary> /// 获取总的记录数 /// </summary> /// <returns></returns> public int GetRecordCount() { string sql = "select count(*)from gb_data"; string res = sqlhelper.ExecuteScalar(sql, CommandType.Text); return Convert.ToInt32(res); } #endregion
pagebar.cs(这个类文件是做的数字分页用的,直接使用就可以了)对应前端代码
/// <summary> /// 获取数字页码条 /// </summary> /// <param name="pageIndex">当前页码值</param> /// <param name="pageCount">总的页数</param> /// <returns></returns> 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("<a href='?pageIndex=1'>首页</a>")); if (pageIndex>1) { sb.Append(string.Format("<a href='?pageIndex={0}'>上一页</a>",pageIndex-1)); } for (int i = start; i <= end; i++) { if (i==pageIndex)//如果循环的数字与当前页码相等,就不需要加超链接 { sb.Append(i); } else { sb.Append(string.Format("<a href='?pageIndex={0}'>{0}</a>", i));//不写页面名字,方便别的页面调用 } } if (pageIndex <pageCount) { sb.Append(string.Format("<a href='?pageIndex={0}'>下一页</a>", pageIndex + 1)); } sb.Append(string.Format("<a href='?pageIndex={0}'>尾页</a>",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 }