General paging function implemented by Asp. Net

  • 2021-07-21 08:11:07
  • OfStack

In this paper, an example is given to describe the general paging function implemented by Asp. Net. Share it for your reference, as follows:

Functions:

1. Each page is set to display 9 pages. If it exceeds 9 pages, click +1 page after 5 pages (you can modify it at will)
2. CSS style can be set by itself
3. No code is generated, which is beneficial to search engine optimization

Paging program


objPDS = new PagedDataSource();
objPDS.DataSource = dtTable.DefaultView;// Bind data source 
objPDS.AllowPaging = true;
objPDS.PageSize =10;// Number of pages 
int curPage;
int cshi;
int jshi;
int zyes = Int32.Parse( objPDS.PageCount.ToString());
this.tjixx.Text = " Total  <font color=red>" + dtTable.Rows.Count + " </font> Items of information ";
this.tjixx.Text += "| Altogether  <font color=red>" + zyes + " </font> Page ";
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;// Bind paged data 
this.DataList4.DataBind();

Aspx file:

Paging section:


<div align="center"> <asp:HyperLink ID="linkFirstPage" runat="server" Font-Underline="False"> Home page  </asp:HyperLink>&lt;&lt;
<asp:HyperLink ID="linkPre" runat="server" Font-Underline="False"> Upper 1 Page  </asp:HyperLink>
<asp:Literal ID="yemsl" runat="server"> </asp:Literal>
<asp:HyperLink ID="linkNext" runat="server" Font-Underline="False"> Under 1 Page  </asp:HyperLink>&nbsp; &nbsp;
  &gt;&gt; <asp:HyperLink
  ID="linkEndPage" runat="server" Font-Underline="False"> End page  </asp:HyperLink>| <asp:Literal
    ID="tjixx" runat="server"> </asp:Literal> </div>

For more readers interested in asp. net, please check the topics on this site: "Summary of File Operation Skills of asp. net", "Summary of Skills of asp. net ajax" and "Summary of Cache Operation Skills of asp. net".

I hope this article is helpful to everyone's asp. net programming.


Related articles: