ASP.NET分页组件学与用——教学篇

** ASP.NET ** ** 分页组件学与用——教学篇 ** ** **

没有人会怀疑分页组件在 WEB 应用程序中的作用。数据库中的记录数成千上万甚至过亿,如果一股脑儿显示在一页显然毫不现实,这样的程序员也太小儿科了。所以,最好的办法就是分页显示,每页只显示数据库中的一部分记录,可以翻页,也可以输入一个页码翻到指定的页面,这种方式也是当前比较常见的用法。

本文的不同之处在于,我把分页的功能封装在组件中,一方面体现了面向对象的特点,另一方面也方便发布、共享和使用。事先声明,本文不再讲述组件创建的详细过程,如果有疑点请参考本 BLOG 其他相关文章( asp.net 组件设计浅论, ASP.NET 组件编程 step by step )。

首先来看看该组件的外观:

该组件最后运行显示在客户端其实是一个表格,表格被分成三段,第一段是与页和记录相关的信息;第二段是页导航,该导航显示一组带超链接的数字,通过点击数字可以转移到对应的页;第三段有两个 HTML 控件,用户可以输入数字转移到指定的页。从图中也可以看出,该组件的功能非常简单明了。

首先我们来关注第一部分。这一部分信息包括:当前页,总页数,每页显示的记录条数和总的记录条数。这些信息从组件外部传进来,所以我们定义对应的属性:

private int _count; //每页显示的记录条数

private int _currentPage; //当前页

private int _allCount; //所有记录条数

private int _showPages; //显示数字个数

我在注释 _showPages这个属性的时候有点晦涩,所以需要简单的讲一下:该属性用来定义数字导航栏显示的数字个数,在上面的图中,定义显示10个数字,即从201——210,当然,根据需要,我们可以定义任意多个数字。

[DefaultValue(10),Category( "Customer" )]

public int Count

{

set

{

if ( value <= 0)

_count = 1;

else

_count = value ;

}

get

{

return _count;

}

}

[DefaultValue(1),Category( "Customer" )]

public int CurrentPage

{

set

{

if ( value < 0)

_currentPage = 1;

else

_currentPage = value ;

}

get

{

return _currentPage;

}

}

[DefaultValue(1),Category( "Customer" )]

public int AllCount

{

get

{

return _allCount;

}

set

{

if (_allCount < 0)

throw new Exception( "记录总条数不能为负数" );

else

_allCount = value ;

}

}

[DefaultValue(1),Category( "Customer" )]

public int Pages //总页数

{

get

{

if ( this ._allCount % this ._count > 0)

return (( int ) this ._allCount / this ._count) + 1;

else

return (( int ) this ._allCount / this ._count);

}

}

[DefaultValue(1),Category( "Customer" )]

public int ShowPages

{

set

{

if ( value > 0)

_showPages = value ;

else

_showPages = 9;

}

get

{

return _showPages;

}

}

在定义的属性中,有一个叫 Pages的属性,该属性不需要从外面传值,而过计算出来的。他的值等于总记录条数除以每页显示的记录条数(具体请见代码)。

现在我们要把这些值显示出来,用下面的代码显示:

//分页条分三部分,leftInfo是最左边的部分,用来显示当前页/总页数,每页显示的记录条数

leftInfo = "页:" + this .CurrentPage.ToString() + "/" + this .Pages.ToString() + "  " + "每页" + this .Count.ToString() + "条" + "  共" + this .AllCount.ToString() + "条" ;

第二段比较复杂。组件的页面导航数字是连续的,所以,我定义了一个最小值和最大值:

int min; //要显示的页面导航数最小值

int max; //要显示的页面导航数最大值

设计时,需要考虑三种情况:

1:如果当前页除以ShowPages余数为0,也就是恰好可以整除的话,页面导航数字最小值和最大值分别是:

min最小值 = 当前页 + 1

max最大值 = 当前页 + 页面导航数字个数(ShowPages)

对应代码:

if ( this .CurrentPage % this .ShowPages == 0) //如果恰好整除

{

min = this .CurrentPage + 1;

max = this .CurrentPage + this .ShowPages ;

}

2:如果当前页是导航数字最小值时,应该切换到前一组导航数字。此时,导航数字的最小值和最大值分别是:

min最小值 = ( ((int)当前页 / 页面导航数字个数ShowPages ) - 1) *页面导航数字个数ShowPages +1;

max最大值 = 当前页 – 1

对应代码如下:

else if ( this .CurrentPage % this .ShowPages == 1 && this .CurrentPage > this .ShowPages )

{

min = ((( int ) this .CurrentPage / this .ShowPages ) - 1) * this .ShowPages +1;

max = this .CurrentPage - 1;

}

3:如果当前页是导航数字最大值时,应该切换到后一组导航数字。此时,导航数字的最小值和最大值分别是:

min最小值 = ((int)当前页 / 页面导航数字个数 ShowPages) * 页面导航数字个数 ShowPages + 1

max最大值 = (((int)当前页 / 页面导航数字个数 ShowPages) +1) * 页面导航数字个数 ShowPages

对应代码如下:

else

{

min = (( int ) this .CurrentPage / this .ShowPages) * this .ShowPages + 1;

max = ((( int ) this .CurrentPage / this .ShowPages) +1) * this .ShowPages;

}

即然导航数字列表的最小值和最大值都计算出来了,所以我们通个做一个循环操作就可以生成该导航,当前页用 斜体 和 红色 字体突出显示:

for ( int i = min ; i <= max ; i++)

{

if (i <= this .Pages) //只有不大于最大页才显示

{

if ( this .CurrentPage == i) //如果是当前页,用斜体和红色显示

{

numberStr = numberStr + "

1<a ?currentpage="  \+ i.ToString() +  " href="  \+ AbsUrl +  ">"  \+  "<i style="color:red">"  \+ i.ToString() +  "</i>"  \+  "</a>

" + "\n" ;

}

else

{

numberStr = numberStr + "

1<a ?currentpage="  \+ i.ToString() +  " href="  \+ AbsUrl +  ">"  \+ i.ToString() +  "</a>

" + "\n" ;

}

}

}

大家应该看出来了,在导航列表的最前面和最后面一共还有四个图标,这几个图标并不是图片,而是 7348四个数字的Wedding字体。这四个图标的代码如下:

//第一页,上一页,下一页,最后一页

string First,Previous,Next,Last;

First = AbsUrl + "?currentPage=1" ;

/////////

if ( this .CurrentPage == 1)

Previous = AbsUrl + "?currentPage=1" ;

else

<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIG

Published At
Categories with Web编程
Tagged with
comments powered by Disqus