本文實例講述了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>
更多關於asp.net相關內容感興趣的讀者可查看本站專題:《asp.net文件操作技巧匯總》、《asp.net ajax技巧總結專題》及《asp.net緩存操作技巧總結》。
希望本文所述對大家asp.net程序設計有所幫助。