本文实例讲述了Asp.Net实现的通用分页函数。分享给大家供大家参考,具体如下:
功能:
1.每页设置显示9页,超过9页,点5页后的+1页显示(可以随便修改)
2.CSS样式自己可以设置
3.无任何咋代码产生,利于搜索引擎优化
分页程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
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文件:
分页部分:
1
2
3
4
5
6
7
|
<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程序设计有所帮助。