本文實(shí)例講述了Asp.Net實(shí)現(xiàn)的通用分頁(yè)函數(shù)。分享給大家供大家參考,具體如下:
功能:
1.每頁(yè)設(shè)置顯示9頁(yè),超過(guò)9頁(yè),點(diǎn)5頁(yè)后的+1頁(yè)顯示(可以隨便修改)
2.CSS樣式自己可以設(shè)置
3.無(wú)任何咋代碼產(chǎn)生,利于搜索引擎優(yōu)化
分頁(yè)程序
objPDS = new PagedDataSource();
objPDS.DataSource = dtTable.DefaultView;//綁定數(shù)據(jù)源
objPDS.AllowPaging = true;
objPDS.PageSize =10;//分頁(yè)數(shù)目
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>頁(yè)";
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;//綁定分頁(yè)的數(shù)據(jù)
this.DataList4.DataBind();
Aspx文件:
分頁(yè)部分:
<div align="center"> <asp:HyperLink ID="linkFirstPage" runat="server" Font-Underline="False">首頁(yè) </asp:HyperLink><<
<asp:HyperLink ID="linkPre" runat="server" Font-Underline="False">上一頁(yè) </asp:HyperLink>
<asp:Literal ID="yemsl" runat="server"> </asp:Literal>
<asp:HyperLink ID="linkNext" runat="server" Font-Underline="False">下一頁(yè) </asp:HyperLink>
>> <asp:HyperLink
ID="linkEndPage" runat="server" Font-Underline="False">尾頁(yè) </asp:HyperLink>| <asp:Literal
ID="tjixx" runat="server"> </asp:Literal> </div>
希望本文所述對(duì)大家asp.net程序設(shè)計(jì)有所幫助。