在asp代码中分页是有点麻烦的事情,个人在在代码编写过程中把分页代码写成了两个函数,虽然在功能上不是很完善,但对于一般的应用应该是满足的了。 

<%

'分页函数分为两个函数

'CalcPage(totalrec,msg_per_page,currentpage,n,rowcount,PageRs) 分页计算函数

'PageList(ListType,url,querry,Separator,ListLink) 分页列表函数



'分页计算函数

'totalrec 记录集总数

'msg_per_page 每页显示的记录数,在调用CalcPage时需提前对该变量赋值

'currentpage 当前页变量,在调用CalcPage时需提前对该变量赋值

'n 总页数  

'rowcount 设置每一页的数据记录数

'PageRs 记录集对象

sub CalcPage(totalrec,msg_per_page,currentpage,n,rowcount,PageRs)

 n=0 '设置无记录时页数为0 

 if currentpage="" then currentpage=0

 'PageRs.EOF and PageRs.bof  无记录

 'Not PageRs.EOF Or Not PageRs.BOF 有记录

 if Not PageRs.EOF Or Not PageRs.BOF then 

  totalrec=PageRs.recordcount

  PageRs.pagesize=msg_per_page

  if totalrec mod msg_per_page = 0 then '计算总页数,recordcount:数据的总记录数

   n = totalrec\msg_per_page 'n:总页数

  else 

   n = totalrec\msg_per_page+1 

  end if 

  if not isnumeric(currentpage) or currentpage="" then currentpage=1

  If currentpage <> "" then

   currentpage = cint(currentpage)

  end if

  if currentpage < 1 then 

   currentpage = 1

  end if 

  if currentpage*msg_per_page > totalrec and not((currentpage-1)*msg_per_page < totalrec) then 

   currentPage=1

  end if

  PageRs.absolutepage = currentpage 'absolutepage:设置指针指向某页开头

  rowcount = PageRs.pagesize            'pagesize:设置每一页的数据记录数

 end if

end sub

%>

<%

'分页列表函数

'url 跳转的地址

'querry ?后的参数

'Separator 分隔符

'ListType 分页类型

'类型:0 "第一页 | 前一页 | 下一页 | 最后页"

'类型:1 "1 | 2 | 3 | 4 | ..........| 下一页"

'类型:2 "第一页 | 前十页 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 后十页 | 最后页"

'ListLink 链接使用的样式



sub PageList(ListType,url,querry,Separator,ListLink)

 if Separator="" then Separator="|"

 if ListType="" then ListType="0"

 select case ListType

  case "0"

   response.write"第"&currentpage&"/"&n&"页&nbsp;&nbsp;" 

   response.write"共"&totalrec&"条信息&nbsp;&nbsp;"

   if currentpage <= 1 then

   response.write"第一页&nbsp;"&Separator&"&nbsp;"

   response.write"前一页&nbsp;"&Separator&"&nbsp;"

   else

   response.write"<a href="""&url&"?page=1&"&querry&""" class="""&ListLink&""">第一页</a>&nbsp;"&Separator&"&nbsp;" 

   response.write"<a href="""&url&"?page="&currentpage-1&"&"&querry&"""  class="""&ListLink&""">前一页</a>&nbsp;"&Separator&"&nbsp;" 

   end if

   if currentpage = n then

   response.write"下一页&nbsp;"&Separator&"&nbsp;"

   response.write"最后页&nbsp;"

   else

   response.write"<a href="""&url&"?page="&currentpage+1&"&"&querry&"""  class="""&ListLink&""">下一页</a>&nbsp;"&Separator&"&nbsp;"

   response.write"<a href="""&url&"?page="&n&"&"&querry&""" class="""&ListLink&""">最后页</a>&nbsp;"

   end if

  case "1"

   if currentpage < n then

    response.write"<a href="""&url&"?page="&currentpage+1&"&"&querry&""" class="""&ListLink&""">下一页</a>&nbsp;"    

   else

    response.write"下一页&nbsp;"    

   end if

   for i=1 to n

    if cstr(i)=cstr(currentpage) then

     response.write "<b>"&i&"</b>"&"&nbsp;"&Separator&"&nbsp;"

     else

      response.write"<a href="""&url&"?page="&i&"&"&querry&""" class="""&ListLink&""">"&i&"</a>&nbsp;"&Separator&"&nbsp;"  

    end if

   next

   

  case "2"

   PageMerCout=10 '每次可翻的最大页数

   '取得记录的最大页码段

   if n mod PageMerCout=0 then

    MaxPageFiled=n\PageMerCout

   else

    MaxPageFiled=n\PageMerCout+1

   end if

   '判断当前页所在的页码段

   if currentpage mod PageMerCout =0 then

    CurrPageFiled=currentpage\PageMerCout

   else

    CurrPageFiled=currentpage\PageMerCout+1

   end if

   '取得当前页码段的最大页码和最小页码

   MaxPageNo=CurrPageFiled*PageMerCout

   MinPageNo=(CurrPageFiled-1)*PageMerCout+1

   '输出 “第一页 | 前十页 |”

   if currentpage<=1 then

    response.write"第一页&nbsp;"&Separator&"&nbsp;" 

   else

    response.write"<a href="""&url&"?page=1&"&querry&""" class="""&ListLink&""">第一页</a>&nbsp;"&Separator&"&nbsp;" 

   end if

   if CurrPageFiled<=1 then

    response.write"前十页&nbsp;"&Separator&"&nbsp;"       

   else

    response.write"<a href="""&url&"?page="&MinPageNo-PageMerCout&"&"&querry&""" class="""&ListLink&""">前十页</a>&nbsp;"&Separator&"&nbsp;"       

   end if

   '输出当前页码段

   for i=MinPageNo to MaxPageNo

    if i<=n then

     if cstr(i)=cstr(currentpage) then

      response.write "<b>"&i&"</b>"&"&nbsp;"&Separator&"&nbsp;"

      else

       response.write"<a href="""&url&"?page="&i&"&"&querry&""">"&i&"</a>&nbsp;"&Separator&"&nbsp;"  

     end if

    end if

   next

   '输出 “后十页 | 最后页”

   if CurrPageFiled>=MaxPageFiled then

    response.write"后十页&nbsp;"&Separator&"&nbsp;"       

   else

    response.write"<a href="""&url&"?page="&MaxPageNo+1&"&"&querry&""" class="""&ListLink&""">后十页</a>&nbsp;"&Separator&"&nbsp;"       

   end if

   if currentpage>=n then

    response.write"最后页&nbsp;"  

   else

    response.write"<a href="""&url&"?page="&n&"&"&querry&""" class="""&ListLink&""">最后页</a>&nbsp;"  

   end if

 end select

end sub

%>

 

比如本程序实现共四个文件:

分别如下:

数据库:data.mdb

表名:table

字段分别为:Id、Title、Content

注意:Content为类型为备注类型,SQL SERVER中为text类型

数据库接口文件:conn.asp

主显示页:show.asp

公共方法页:syscode.asp

------------------------------------


syscode.asp

<%

'=================================================

'代码移植优化作者:cleanwater

'作者站点:http://www.cn086.com

'嵌入调用方法:<!--#include file="syscode.asp"-->

'公共调用方法:call ShowContent("参数","参数","参数")

'公共设置参数:ShowContentByPage、MaxPerPage_Content

'注:如做多领域调用处理直接将两个参数移植到需调用的页面即可

'=================================================

Const ShowContentByPage="Yes"        '文本内容是否分页显示

Const MaxPerPage_Content=1400        '每页显示大约字符数

'=================================================

'过程名:ShowContent

'作  用:显示文本具体的内容,可以分页显示

'参  数:无

'注:以下代码无任何特殊情况请勿随便改动

'=================================================

sub ShowContent(rsID,rsexplain,rswebpage)

dim ID,strContent,CurrentPage

dim ContentLen,MaxPerPage,pages,i,lngBound

dim BeginPoint,EndPoint

ID=rsID'获取递值文本ID

strContent=rsexplain'获取递值文本内容

webpage=rswebpage'获取递值页名

ContentLen=len(strContent)

CurrentPage=trim(request("sPage"))

if ShowContentByPage="No" or ContentLen<=MaxPerPage_Content then

response.write strContent

if ShowContentByPage="Yes" then

response.write "</p><p align='center'><font color='red'><b>[1]</b></font></p>"

end if

else

if CurrentPage="" then

CurrentPage=1

else

CurrentPage=Cint(CurrentPage)

end if

pages=ContentLen\MaxPerPage_Content

if MaxPerPage_Content*pages<ContentLen then

pages=pages+1

end if

lngBound=MaxPerPage_Content          '最大误差范围

if CurrentPage<1 then CurrentPage=1

if CurrentPage>pages then CurrentPage=pages


dim lngTemp

dim lngTemp1,lngTemp1_1,lngTemp1_2,lngTemp1_1_1,lngTemp1_1_2,lngTemp1_1_3,lngTemp1_2_1,lngTemp1_2_2,lngTemp1_2_3

dim lngTemp2,lngTemp2_1,lngTemp2_2,lngTemp2_1_1,lngTemp2_1_2,lngTemp2_2_1,lngTemp2_2_2

dim lngTemp3,lngTemp3_1,lngTemp3_2,lngTemp3_1_1,lngTemp3_1_2,lngTemp3_2_1,lngTemp3_2_2

dim lngTemp4,lngTemp4_1,lngTemp4_2,lngTemp4_1_1,lngTemp4_1_2,lngTemp4_2_1,lngTemp4_2_2

dim lngTemp5,lngTemp5_1,lngTemp5_2

dim lngTemp6,lngTemp6_1,lngTemp6_2


if CurrentPage=1 then

BeginPoint=1

else

BeginPoint=MaxPerPage_Content*(CurrentPage-1)+1


lngTemp1_1_1=instr(BeginPoint,strContent,"</table>",1)

lngTemp1_1_2=instr(BeginPoint,strContent,"</TABLE>",1)

lngTemp1_1_3=instr(BeginPoint,strContent,"</Table>",1)

if lngTemp1_1_1>0 then

lngTemp1_1=lngTemp1_1_1

elseif lngTemp1_1_2>0 then

lngTemp1_1=lngTemp1_1_2

elseif lngTemp1_1_3>0 then

lngTemp1_1=lngTemp1_1_3

else

lngTemp1_1=0

end if


lngTemp1_2_1=instr(BeginPoint,strContent,"<table",1)

lngTemp1_2_2=instr(BeginPoint,strContent,"<TABLE",1)

lngTemp1_2_3=instr(BeginPoint,strContent,"<Table",1)

if lngTemp1_2_1>0 then

lngTemp1_2=lngTemp1_2_1

elseif lngTemp1_2_2>0 then

lngTemp1_2=lngTemp1_2_2

elseif lngTemp1_2_3>0 then

lngTemp1_2=lngTemp1_2_3

else

lngTemp1_2=0

end if


if lngTemp1_1=0 and lngTemp1_2=0 then

lngTemp1=BeginPoint

else

if lngTemp1_1>lngTemp1_2 then

lngtemp1=lngTemp1_2

else

lngTemp1=lngTemp1_1+8

end if

end if


lngTemp2_1_1=instr(BeginPoint,strContent,"</p>",1)

lngTemp2_1_2=instr(BeginPoint,strContent,"</P>",1)

if lngTemp2_1_1>0 then

lngTemp2_1=lngTemp2_1_1

elseif lngTemp2_1_2>0 then

lngTemp2_1=lngTemp2_1_2

else

lngTemp2_1=0

end if


lngTemp2_2_1=instr(BeginPoint,strContent,"<p",1)

lngTemp2_2_2=instr(BeginPoint,strContent,"<P",1)

if lngTemp2_2_1>0 then

lngTemp2_2=lngTemp2_2_1

elseif lngTemp2_2_2>0 then

lngTemp2_2=lngTemp2_2_2

else

lngTemp2_2=0

end if


if lngTemp2_1=0 and lngTemp2_2=0 then

lngTemp2=BeginPoint

else

if lngTemp2_1>lngTemp2_2 then

lngtemp2=lngTemp2_2

else

lngTemp2=lngTemp2_1+4

end if

end if


lngTemp3_1_1=instr(BeginPoint,strContent,"</ur>",1)

lngTemp3_1_2=instr(BeginPoint,strContent,"</UR>",1)

if lngTemp3_1_1>0 then

lngTemp3_1=lngTemp3_1_1

elseif lngTemp3_1_2>0 then

lngTemp3_1=lngTemp3_1_2

else

lngTemp3_1=0

end if


lngTemp3_2_1=instr(BeginPoint,strContent,"<ur",1)

lngTemp3_2_2=instr(BeginPoint,strContent,"<UR",1)

if lngTemp3_2_1>0 then

lngTemp3_2=lngTemp3_2_1

elseif lngTemp3_2_2>0 then

lngTemp3_2=lngTemp3_2_2

else

lngTemp3_2=0

end if


if lngTemp3_1=0 and lngTemp3_2=0 then

lngTemp3=BeginPoint

else

if lngTemp3_1>lngTemp3_2 then

lngtemp3=lngTemp3_2

else

lngTemp3=lngTemp3_1+5

end if

end if


if lngTemp1<lngTemp2 then

lngTemp=lngTemp2

else

lngTemp=lngTemp1

end if

if lngTemp<lngTemp3 then

lngTemp=lngTemp3

end if


if lngTemp>BeginPoint and lngTemp<=BeginPoint+lngBound then

BeginPoint=lngTemp

else

lngTemp4_1_1=instr(BeginPoint,strContent,"</li>",1)

lngTemp4_1_2=instr(BeginPoint,strContent,"</LI>",1)

if lngTemp4_1_1>0 then

lngTemp4_1=lngTemp4_1_1

elseif lngTemp4_1_2>0 then

lngTemp4_1=lngTemp4_1_2

else

lngTemp4_1=0

end if


lngTemp4_2_1=instr(BeginPoint,strContent,"<li",1)

lngTemp4_2_1=instr(BeginPoint,strContent,"<LI",1)

if lngTemp4_2_1>0 then

lngTemp4_2=lngTemp4_2_1

elseif lngTemp4_2_2>0 then

lngTemp4_2=lngTemp4_2_2

else

lngTemp4_2=0

end if


if lngTemp4_1=0 and lngTemp4_2=0 then

lngTemp4=BeginPoint

else

if lngTemp4_1>lngTemp4_2 then

lngtemp4=lngTemp4_2

else

lngTemp4=lngTemp4_1+5

end if

end if


if lngTemp4>BeginPoint and lngTemp4<=BeginPoint+lngBound then

BeginPoint=lngTemp4

else

lngTemp5_1=instr(BeginPoint,strContent,"<img",1)

lngTemp5_2=instr(BeginPoint,strContent,"<IMG",1)

if lngTemp5_1>0 then

lngTemp5=lngTemp5_1

elseif lngTemp5_2>0 then

lngTemp5=lngTemp5_2

else

lngTemp5=BeginPoint

end if


if lngTemp5>BeginPoint and lngTemp5<BeginPoint+lngBound then

BeginPoint=lngTemp5

else

lngTemp6_1=instr(BeginPoint,strContent,"<br/>",1)

lngTemp6_2=instr(BeginPoint,strContent,"<BR>",1)

if lngTemp6_1>0 then

lngTemp6=lngTemp6_1

elseif lngTemp6_2>0 then

lngTemp6=lngTemp6_2

else

lngTemp6=0

end if


if lngTemp6>BeginPoint and lngTemp6<BeginPoint+lngBound then

BeginPoint=lngTemp6+4

end if

end if

end if

end if

end if


if CurrentPage=pages then

EndPoint=ContentLen

else

  EndPoint=MaxPerPage_Content*CurrentPage

  if EndPoint>=ContentLen then

EndPoint=ContentLen

  else

lngTemp1_1_1=instr(EndPoint,strContent,"</table>",1)

lngTemp1_1_2=instr(EndPoint,strContent,"</TABLE>",1)

lngTemp1_1_3=instr(EndPoint,strContent,"</Table>",1)

if lngTemp1_1_1>0 then

lngTemp1_1=lngTemp1_1_1

elseif lngTemp1_1_2>0 then

lngTemp1_1=lngTemp1_1_2

elseif lngTemp1_1_3>0 then

lngTemp1_1=lngTemp1_1_3

else

lngTemp1_1=0

end if


lngTemp1_2_1=instr(EndPoint,strContent,"<table",1)

lngTemp1_2_2=instr(EndPoint,strContent,"<TABLE",1)

lngTemp1_2_3=instr(EndPoint,strContent,"<Table",1)

if lngTemp1_2_1>0 then

lngTemp1_2=lngTemp1_2_1

elseif lngTemp1_2_2>0 then

lngTemp1_2=lngTemp1_2_2

elseif lngTemp1_2_3>0 then

lngTemp1_2=lngTemp1_2_3

else

lngTemp1_2=0

end if


if lngTemp1_1=0 and lngTemp1_2=0 then

lngTemp1=EndPoint

else

if lngTemp1_1>lngTemp1_2 then

lngtemp1=lngTemp1_2-1

else

lngTemp1=lngTemp1_1+7

end if

end if


lngTemp2_1_1=instr(EndPoint,strContent,"</p>",1)

lngTemp2_1_2=instr(EndPoint,strContent,"</P>",1)

if lngTemp2_1_1>0 then

lngTemp2_1=lngTemp2_1_1

elseif lngTemp2_1_2>0 then

lngTemp2_1=lngTemp2_1_2

else

lngTemp2_1=0

end if


lngTemp2_2_1=instr(EndPoint,strContent,"<p",1)

lngTemp2_2_2=instr(EndPoint,strContent,"<P",1)

if lngTemp2_2_1>0 then

lngTemp2_2=lngTemp2_2_1

elseif lngTemp2_2_2>0 then

lngTemp2_2=lngTemp2_2_2

else

lngTemp2_2=0

end if


if lngTemp2_1=0 and lngTemp2_2=0 then

lngTemp2=EndPoint

else

if lngTemp2_1>lngTemp2_2 then

lngTemp2=lngTemp2_2-1

else

lngTemp2=lngTemp2_1+3

end if

end if


lngTemp3_1_1=instr(EndPoint,strContent,"</ur>",1)

lngTemp3_1_2=instr(EndPoint,strContent,"</UR>",1)

if lngTemp3_1_1>0 then

lngTemp3_1=lngTemp3_1_1

elseif lngTemp3_1_2>0 then

lngTemp3_1=lngTemp3_1_2

else

lngTemp3_1=0

end if


lngTemp3_2_1=instr(EndPoint,strContent,"<ur",1)

lngTemp3_2_2=instr(EndPoint,strContent,"<UR",1)

if lngTemp3_2_1>0 then

lngTemp3_2=lngTemp3_2_1

elseif lngTemp3_2_2>0 then

lngTemp3_2=lngTemp3_2_2

else

lngTemp3_2=0

end if


if lngTemp3_1=0 and lngTemp3_2=0 then

lngTemp3=EndPoint

else

if lngTemp3_1>lngTemp3_2 then

lngtemp3=lngTemp3_2-1

else

lngTemp3=lngTemp3_1+4

end if

end if


if lngTemp1<lngTemp2 then

lngTemp=lngTemp2

else

lngTemp=lngTemp1

end if

if lngTemp<lngTemp3 then

lngTemp=lngTemp3

end if


if lngTemp>EndPoint and lngTemp<=EndPoint+lngBound then

EndPoint=lngTemp

else

lngTemp4_1_1=instr(EndPoint,strContent,"</li>",1)

lngTemp4_1_2=instr(EndPoint,strContent,"</LI>",1)

if lngTemp4_1_1>0 then

lngTemp4_1=lngTemp4_1_1

elseif lngTemp4_1_2>0 then

lngTemp4_1=lngTemp4_1_2

else

lngTemp4_1=0

end if


lngTemp4_2_1=instr(EndPoint,strContent,"<li",1)

lngTemp4_2_1=instr(EndPoint,strContent,"<LI",1)

if lngTemp4_2_1>0 then

lngTemp4_2=lngTemp4_2_1

elseif lngTemp4_2_2>0 then

lngTemp4_2=lngTemp4_2_2

else

lngTemp4_2=0

end if


if lngTemp4_1=0 and lngTemp4_2=0 then

lngTemp4=EndPoint

else

if lngTemp4_1>lngTemp4_2 then

lngtemp4=lngTemp4_2-1

else

lngTemp4=lngTemp4_1+4

end if

end if


if lngTemp4>EndPoint and lngTemp4<=EndPoint+lngBound then

EndPoint=lngTemp4

else

lngTemp5_1=instr(EndPoint,strContent,"<img",1)

lngTemp5_2=instr(EndPoint,strContent,"<IMG",1)

if lngTemp5_1>0 then

lngTemp5=lngTemp5_1-1

elseif lngTemp5_2>0 then

lngTemp5=lngTemp5_2-1

else

lngTemp5=EndPoint

end if


if lngTemp5>EndPoint and lngTemp5<EndPoint+lngBound then

EndPoint=lngTemp5

else

lngTemp6_1=instr(EndPoint,strContent,"<br/>",1)

lngTemp6_2=instr(EndPoint,strContent,"<BR>",1)

if lngTemp6_1>0 then

lngTemp6=lngTemp6_1+3

elseif lngTemp6_2>0 then

lngTemp6=lngTemp6_2+3

else

lngTemp6=EndPoint

end if


if lngTemp6>EndPoint and lngTemp6<EndPoint+lngBound then

EndPoint=lngTemp6

end if

end if

end if

end if

  end if

end if

response.write mid(strContent,BeginPoint,EndPoint-BeginPoint)


response.write "</p><p align='center'><b>"

if CurrentPage>1 then

response.write "<a href='" & webpage & "?ID=" & ID & "&sPage=" & CurrentPage-1 & "'>上一页</a>&nbsp;&nbsp;"

end if

for i=1 to pages

if i=CurrentPage then

response.write "<font color='red'>[" & cstr(i) & "]</font>&nbsp;"

else

response.write "<a href='" & webpage & "?ID=" & ID & "&sPage=" & i & "'>[" & i & "]</a>&nbsp;"

end if

next

if CurrentPage<pages then

response.write "&nbsp;<a href='" & webpage & "?ID=" & ID & "&sPage=" & CurrentPage+1 & "'>下一页</a>"

end if

response.write "</b></p>"

end if


end sub

%>




开始演示

------------show.asp页代码如下------------------------


<!--#include file="conn.asp"-->

<% set rs=server.CreateObject("adodb.recordset")

Id=request("Id")'获取相应ID

sql="SELECT * FROM table where Id="& request("Id")

rs.open sql,conn,1,1

%>

<!--#include file="syscode.asp"-->

<html>

<head>

<title>详细内容显示</title>

<meta http-equiv="content-type" content="text/html; charset=gb2312">

</head>

<body>

<table width="100%">

<tr>

<td style="word-break:break-all;">

<p align="center">

<b>标题:<%=rs("Title")%></b>

</p>

<br/>

<p>

<%call ShowContent(""&rs("Id")&"",""&rs("Content")&"","show.asp")%>

</p>

</td>

</tr>

</table>

</body>

</html>

 

<%

'连接数据库

dim conn,connstr

on error resume next



set conn=server.createobject("adodb.connection")

connstr="provider=microsoft.jet.oledb.4.0;Data source="&server.mappath("../data/#.mdb")

conn.open connstr



if err.number<>0 then

response.write err.description

err.clear

response.end

end if


sub connclose()

conn.close()

set conn=nothing

end sub


'读取数据:

dim rs,sql,content,title,newsid

id=request("id") '上页传来的ID值

set rs=server.createobject("adodb.recordset")

sql="select * from news where newsid="&id

rs.open sql,conn,1,1

if not(rs.eof and rs.bof) then

content=rs("content") '读取内容

title=rs("title") '读取标题

end if



if err.number<>0 then

response.write err.description

err.clear

response.end

end if

rs.close

set rs=nothing



call connclose()

'分页处理部分

dim page,pagecount,thispage,linenum,allline

const pageline=10 '每页显示10行

linenum=split(content,"<br>") '计算机字符串<br>标记的个数

allline=ubound(linenum)+1 '全文<br>(换行标记)总数

pagecount=int(allline\pageline)+1 '计算总页数

page=request("page")



if isempty(page) then

thispage=1

else

thispage=cint(page)

end if



response.write "<b>"&title&"</b><hr>"

for i=0 to allline

if i+1>thispage*pageline-pageline and i<thispage*pageline then

response.write""&linenum(i)&"<br>" '输出分页后的内容

end if

next

response.write "<br><hr>"

response.write"<p align='center'>总共"&allline&"行"&pagecount&"页每页"&pageline&"行"

for i=1 to pagecount

if thispage=i then

response.write ""&i&" "

else

response.write"<a href='?page="&i&"&id="&id&"'>"&i&"</a>"

'输出所有分页链接

end if

next

%>

  1. 1