本文实例讲述了Asp.Net实现的通用分页函数。分享给大家供大家参考,具体如下:
功能:
1.每页设置显示9页,超过9页,点5页后的+1页显示(可以随便修改)
2.CSS样式自己可以设置
3.无任何咋代码产生,利于搜索引擎优化
分页程序
objPDS = new PagedDataSource();objPDS.DataSource = dtTable.DefaultView;//绑定数据源objPDS.AllowPaging = true;objPDS.PageSize =10;//分页数目int curPage;int cshi;int jshi;int zyes = Int32.Parse( objPDS.PageCount.ToString());this.tjixx.Text = "总共 <font color=red>" + dtTable.Rows.Count + " </font>条信息";this.tjixx.Text += "|共 <font color=red>" + zyes + " </font>页";if (Request.QueryString["Page"] != null){ if (Int32.Parse(Request.QueryString["Page"]) > zyes) curPage = zyes; else curPage = Int32.Parse(Request.QueryString["Page"]);}else{ curPage = 1;}if (zyes <= 9){ cshi = 1; jshi = zyes;}else{ if (curPage <= 5) { cshi = 1; jshi = 9; }else{ cshi = curPage - 4; int jshils = curPage + 4; if (jshils > zyes) jshi = zyes; else jshi=curPage + 4;}}objPDS.CurrentPageIndex = curPage - 1;StringBuilder m_strPageInfo = new StringBuilder();for (int i = cshi; i <=jshi; i++){ if (i == Int32.Parse(curPage.ToString())) m_strPageInfo.Append(" <span class=/"dqye/"> <strong>" + i + " </strong> </span> "); else m_strPageInfo.Append(" <span class=/"qtye/"> <strong> <a href=/"newslist_ej" + Request.QueryString["wzcat"] + "_Page" + i + ".html/">" + i + " </a> </strong> </span> ");}this.yemsl.Text = m_strPageInfo.ToString();if (!objPDS.IsFirstPage){ linkPre.NavigateUrl = "newslist_ej" + Request.QueryString["wzcat"] + "_Page" + Convert.ToString(curPage - 1); linkPre.NavigateUrl += ".html";}if (!objPDS.IsLastPage){ linkNext.NavigateUrl = "newslist_ej" + Request.QueryString["wzcat"] + "_Page" + Convert.ToString(curPage + 1); linkNext.NavigateUrl += ".html";}linkFirstPage.NavigateUrl = "newslist_ej" + Request.QueryString["wzcat"] + "_Page1";linkFirstPage.NavigateUrl += ".html";linkEndPage.NavigateUrl = "newslist_ej" + Request.QueryString["wzcat"] + "_Page" + objPDS.PageCount.ToString();linkEndPage.NavigateUrl += ".html";this.DataList4.DataSource = objPDS;//绑定分页的数据this.DataList4.DataBind();
Aspx文件:
分页部分:
<div align="center"> <asp:HyperLink ID="linkFirstPage" runat="server" Font-Underline="False">首页 </asp:HyperLink><<<asp:HyperLink ID="linkPre" runat="server" Font-Underline="False">上一页 </asp:HyperLink><asp:Literal ID="yemsl" runat="server"> </asp:Literal><asp:HyperLink ID="linkNext" runat="server" Font-Underline="False">下一页 </asp:HyperLink> >> <asp:HyperLink ID="linkEndPage" runat="server" Font-Underline="False">尾页 </asp:HyperLink>| <asp:Literal ID="tjixx" runat="server"> </asp:Literal> </div>