Asp.Net实现的通用分页函数

编程学习 2021-07-04 22:41www.dzhlxh.cn编程入门
这篇文章主要介绍了Asp.Net实现的通用分页函数,结合实例形势分析了asp.net分页函数的功能,定义及使用技巧,需要的朋友可以参考下

本文实例讲述了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>&lt;&lt;
<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>&nbsp; &nbsp;
  &gt;&gt; <asp:HyperLink
  ID="linkEndPage" runat="server" Font-Underline="False">尾页 </asp:HyperLink>| <asp:Literal
    ID="tjixx" runat="server"> </asp:Literal> </div>

更多关于asp.net相关内容感兴趣的读者可查看本站专题:《》、《》及《》。

希望本文所述对大家asp.net程序设计有所帮助。

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板