回 帖 发 新 帖 刷新版面

主题:[原创][教程]ASP分页数字导航

今天无意中找到了做类似本论坛的分页导航条的方法,拿出来跟大家分享:

        分析:我们需要让导航条的导航页数不变,而导航范围随当前页码改变。还要让当前页码变成红色且不带链接。很显然不能够用一个简单的循环把页码从1到10的列出来。
        想了很久,决定把整个导航代码分为三个部分:1.位于当前页码前的部分;2.当前页码;3.位于当前页码后的部分。
  问题的关键是要注意三个限制:1.导航符合逻辑(说白咯页数不能小于0)  2.符合记录数 3.符合范围限制
  假定已建立记录集合theRs,并设置了数据分页(不要告诉我你这也不会)。下面开始代码!

'****************

dim c1,c2       ‘定义两个变量,分别表示前后两部分的开始页码
dim cc1,cc2        ’分别定义前后两部分的结束页码
    ‘完成以上部分实际是限制导航范围的关键,
    ’下面限制导航条显示15个页码链接,nowPage是当前页数,已经过正确性处理
c1 = nowPage - 7  
c2 = nowPage + 7
cc1 = nowPage - 1
cc2 = nowPage + 1
if c1 < 1 then    '此处很关键,它符合页数不小于1的要求
    c1 = 1
end if
    '开始第一部分的循环***************
    '由于要符合页数不小于1的要求,所以设置循环的step为-1
    for i = cc1 to c1 step -1
        pp = cc1 + c1 - i    'pp是最后显示的数字。这一步不好理解。但如果不这样处理,则会出现…5.4.3.2.1的情况
                                      ‘则不符合我们1.2.3.4.5……的要求。大家可以去试试看。
        Response.write "<a href=回复列表地址?Page=" & pp &">" & pp & "</a>"
    next
    '本页页码加为红色,且不带链接
    Response.write "<font color='red'>" & nowPage & "</font>"
   
'开始后部分的循环***************
    for j =  cc2 to c2
      if j > theRs.pageCount then exit for
      Response.write <a .................................(省略咯)  & j & "</a>"
   next


基本思路就是这样,有问题大家可以提出来交流!
*************************************************************************************
QQ:451297180 欢迎加入啊

回复列表 (共1个回复)

沙发

'也许不用那么麻烦把?
const PAGENUMCOUNT=10
Dim PageNo,TotalPage,fileName
Dim s1,s2,sp
Dim s

fileName=""
PageNo=21
TotalPage=28

sp = (PageNo - (PageNo Mod PAGENUMCOUNT)) / PAGENUMCOUNT
If sp < 1 Then sp = 0
s1 = sp * PAGENUMCOUNT + 1
s2 = sp * PAGENUMCOUNT + PAGENUMCOUNT
If PageNo < s1 Then
    sp = sp - 1
    s1 = sp * PAGENUMCOUNT + 1
    s2 = sp * PAGENUMCOUNT + PAGENUMCOUNT
End If

if PageNo=1 then
    response.write ("<b>")
else
    if s2>PAGENUMCOUNT then
        response.write "<b><a href=""" & fileName & "?Page=1"">&lt;&lt;</a> <a href=""" & fileName & "?Page=" & s1-1 & """>&lt;</a>&nbsp;"
    else
        response.write "<b>"
    end if
end if
for s=s1 to s2
    if s>0 then
        if s=cint(PageNo) then
            response.write "<font color='red'>" & s & "</font>&nbsp;"
        else
            if s=1 then
                response.write "<a href=""" & fileName & "?Page=" & s & """>" & s & "</a>&nbsp;"
            else
                response.write "<a href=""" & fileName & "?Page=" & s & """>" & s & "</a>&nbsp;"
            end if
        end if
        if s=TotalPage then    exit for
    end if
next
if cint(PageNo)=TotalPage then
    response.write ("</b>")
else
    if s<>TotalPage then
        response.write "<a href=""" & fileName & "?Page=" & s & """>&gt;</a> <a href=""" & fileName & "?Page=" & TotalPage & """>&gt;&gt;</a></b>"
    end if
end if 

我来回复

您尚未登录,请登录后再回复。点此登录或注册