曾经在做wap项目的时候使用SQL 语句来实现分页,但是每按下一页或跳转的时候会再次读取数据库,且查询语句比较复杂(忘记怎么写了..需要的自己百度就一堆^^),导致查询速度超慢....(存储模式不会╮(╯_╰)╭..没做这方面的测试)
通过程序对需要分页的数据进行操作,就不需要花费过多的时间对数据库进行查询,但是服务器压力增大...对于少量数据来说依然比较迅速..同时,对于对搜索结果,这样的分页处理则更加方便+准确(以前尝试过SQL的搜索+分页处理...慢到变态..并且出错...o(╯□╰)o)
Code
1
public int ListPerPage=10; //多少行一页
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
4
/// 分页
5
/// </summary>
6
/// <param name="bookList">页面内容</param>
7
/// <param name="currentPage">当前页码</param>
8
public void PageSort(List<TestClass> list, int currentPage)
9![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
10
if (list.Count <= 0)
11
return;
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
int num = (currentPage - 1) * ListPerPage;
14![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
for (int i = num; i < num + ListPerPage; i++)
16![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
17
if (i >= list.Count)
18
break;
19![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
wap.SimpleText((i + 1) + ".");
21
wap.A("xxx.aspx?id=" + list[i].ID, true);
22
wap.SimpleText(list[i].Name);
23
wap.A("", false);
24
wap.BR();
25
}
26
}
27![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
28![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
29
/// 显示第几页
30
/// </summary>
31
/// <param name="totalPage">总页数</param>
32
/// <param name="currentPage">当前页</param>
33
/// <param name="currentUrl">当前URL</param>
34
public void PageLink(int totalPage, int currentPage, string currentUrl)
35![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
36![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
if (currentPage != totalPage)
38![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
39
wap.A(currentUrl + "p=" + (currentPage + 1), true);
40
wap.SimpleText("下页");
41
wap.A("", false);
42
}
43
else
44![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
45
wap.SimpleText("下页");
46
}
47![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
wap.SimpleText(" ");
49![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
for (int i = 0; i < 5; i++)
51![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
52
int page;
53
if (currentPage % 5 != 0)
54
page = currentPage - (currentPage % 5) + i + 1;
55
else
56
page = currentPage - 5 + i + 1;
57![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
if (page > totalPage)
59
break;
60![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
if (page == currentPage)
62![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
63
wap.SimpleText(currentPage.ToString());
64
}
65
else
66![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
67
wap.A(currentUrl + "p=" + page, true);
68
wap.SimpleText(page.ToString());
69
wap.A("", false);
70
}
71
wap.SimpleText(" ");
72
}
73![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
74
if (currentPage != 1)
75![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
76
wap.A(currentUrl + "p=" + (currentPage - 1), true);
77
wap.SimpleText("上页");
78
wap.A("", false);
79
}
80
else
81![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
82
wap.SimpleText("上页");
83
}
84![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
85
wap.BR();
86![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
87
wap.SimpleText("[" + currentPage + "/" + totalPage + "]页");
88
wap.SimpleText(" to");
89
wap.Input("page", "", 3, "");
90
wap.SimpleText("页");
91![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
92
wap.A(currentUrl + "p=$(page)", true);
93
wap.SimpleText("[转到]");
94
wap.A("", false);
95
wap.BR();
96
}
97
看不懂的代码自己幻想吧(O(∩_∩)O)...大概就是显示文本和链接的东西.
这里包括了分页和导航2部分...把所需的全部数据写入缓存,再通过简单的调用即可实现分页..方便快捷,简单易用..(囧TL)