ASP 文章按行数自动分页代码
<
%
'
连接数据库:
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/alibaba.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
=
200
'
trim(request(id)) '上页传来的ID值,为了调试方便此ID值临时赋为1
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
'
---------------------主代码结束--------------------------
%
>
常见分页代码
<
%
If
rs1.recordcount
>
0
Then
'
记录集不为空则处理记录
rs1.pagesize
=
20
'
设置每页显示的记录数
num
=
rs1.recordcount
'
记录总数
pagenum
=
rs1.pagecount
'
页总数
page
=
request(
"
page
"
)
'
获取页码的原始信息
'
处理页码原始信息的开始!
If
page
<>
""
then
page
=
cint
(page)
if
err.number
<>
0
then
err.clear
page
=
1
end
if
if
page
<
1
then
page
=
1
end
if
else
page
=
1
End
if
if
page
*
rs1.pagesize
>
num
and
not
((page
-
1
)
*
rs1.pagesize
<
num)
then
page
=
1
end
if
'
处理页码原始信息的结束!设置当前页码
rs1.absolutepage
=
page
%
>
<
!
--
判断当前页是否是最后一页,并根据判断设置记录的重复显示
-->
<
%
if
page
<>
pagenum
then
lablenum
=
rs1.pagesize
else
lablenum
=
num
-
(page
-
1
)
*
rs1.pagesize
end
if
for
i
=
1
to
lablenum
%
>
<
tr bgcolor
=
"
#FFFFFF
"
>
<
td height
=
"
25
"
><
div align
=
"
center
"
><
%
=
(rs1.Fields.Item(
"
id
"
).Value)%
></
div
></
td
>
<
td
><
div align
=
"
center
"
><
%
=
(rs1.Fields.Item(
"
名称
"
).Value)%
></
div
></
td
>
<
td
><
div align
=
"
center
"
><
%
=
(rs1.Fields.Item(
"
地址
"
).Value)%
></
div
></
td
>
<
td
><
div align
=
"
center
"
><
%
=
(rs1.Fields.Item(
"
类别
"
).Value)%
></
div
></
td
>
<
td
><
div align
=
"
center
"
><
%
=
(rs1.Fields.Item(
"
最后修改
"
).Value)%
></
div
></
td
>
<
td
><
div align
=
"
center
"
><
%
=
(rs1.Fields.Item(
"
修改人
"
).Value)%
></
div
></
td
>
</
tr
>
<
%
rs1.movenext
next
%
>
<
!
--
当前页的记录显示结束,以下代码为记录集分页链接代码
-->
<
table width
=
"
70%
"
border
=
"
0
"
align
=
"
center
"
cellpadding
=
"
0
"
cellspacing
=
"
0
"
>
<
tr
>
<
td height
=
"
35
"
>
<
div align
=
"
right
"
>
<
font color
=
"
#333333
"
>
共有
<
%
=
num%
>
个链接 |
<
a href
=
linkadmin.asp?page
=
1
>
首页
</
a
>
|
<
%
if
page
>
1
then
%
><
a href
=
linkadmin.asp?page
=<
%
=
page
-
1
%
>><
%
end
if
%
>
上一页
</
a
>
|
<
%
if
page
<
pagenum
then
%
><
a href
=
linkadmin.asp?page
=<
%
=
page
+
1
%
>><
%
end
if
%
>
下一页
</
a
>
|
<
a href
=
linkadmin.asp?page
=<
%
=
pagenum%
>>
尾页
</
a
>
|
页次:
<
%
=
page%
>/<
%
=
pagenum%
>
页 |
共
<
%
=
pagenum%
>
页
</
font
>
</
div
>
</
td
>
</
tr
>
</
table
>
<
!
--
记录集分页链接代码结束,记录集为空时执行以下代码
-->
<
%
else
%
>
<
tr bgcolor
=
"
#FFFFFF
"
>
<
td height
=
"
25
"
colspan
=
"
6
"
><
div align
=
"
center
"
><
% response.Write(
"
没有结果可显示!
"
) %
>
</
div
></
td
>
</
tr
>
<
%
end
if
rs1.Close()
Set
rs1
=
Nothing
%
>

网页按字数自动分页代码
<
!
--
#include file
=
"
admin2008exe/conn2008set1.asp
"
-->

<
%
id
=
request.querystring(
"
id
"
)
sql10
=
"
select * from shop_books where id=
"
&
id
set
rs
=
conn2008set.execute(sql10)
'
=================================================
'
过程名:AutoPagination
'
作 用:采用自动分页方式显示文章具体的内容
'
参 数:无
'
=================================================
Call
AutoPagination()

sub
AutoPagination()
dim
ArticleID,strContent,CurrentPage
dim
ContentLen,MaxPerPage,pages,i,lngBound
dim
BeginPoint,EndPoint
ArticleID
=
rs(
"
id
"
)
strContent
=
rs(
"
nrjg
"
)
ContentLen
=
len
(strContent)
CurrentPage
=
trim
(request(
"
ArticlePage
"
))
if
ContentLen
<=
rs(
"
newshop
"
)
then
response.write strContent
response.write
"
</p><p align='center'><font color='red'><b>[1]</b></font></p>
"
else
if
CurrentPage
=
""
then
CurrentPage
=
1
else
CurrentPage
=
Cint
(CurrentPage)
end
if
pages
=
ContentLen
rs(
"
newshop
"
)
if
rs(
"
newshop
"
)
*
pages
<
ContentLen
then
pages
=
pages
+
1
end
if
lngBound
=
ContentLen
'
最大误差范围
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
=
rs(
"
MaxCharPerPage
"
)
*
(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
=
rs(
"
newshop
"
)
*
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
但是我发现有这样的问题
如果是简单的文章
可以根据字数或是行数进行自动分页
但是如果内容中有表格,图片等时
就会出现版面混乱
我想到的办法是
在内容中合适的位置中手动加入断点
然后由系统自动寻找断点并进行自动分页
是麻烦了点
不过是我想到的最好的办法了
大家还有什么更好的办法没啊?
讨论一下^-^-^
为了版美观,有时需要将一编较长的文章分页来显示,这时只好将文章分多次存入数据库,极不方便
本人见过多种自动分页代码,感觉上不是很理想
偶的思路是统计文章的所有行数,按指定行数输出显示内容并生成分页导航
<
%
'
连接数据库:
on
error
resume
next
dim
conn,connstr,dbpath
dbpath
=
server.mappath(
"
web.mdb
"
)
'
数据库文件名
set
conn
=
server.createobject(
"
adodb.connection
"
)
connstr
=
"
driver={microsoft access driver (*.mdb)};dbq=
"
&
dbpath
&
"
;
"
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,conntent,title,id
id
=
1
'
trim(request("id")) '上页传来的ID值,为了调试方便此ID值临时赋为1
set
rs
=
server.createobject(
"
adodb.recordset
"
)
sql
=
"
select * from news where id=
"
&
cint
(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
"
<title>
"
&
title
&
"
</title><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
chr
(
13
)
&
"
<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
'
---------------------主代码结束--------------------------
http://www.sy8.net.ru/article.asp?id=340











































































常见分页代码













































































网页按字数自动分页代码










































































































































































































































































但是我发现有这样的问题
如果是简单的文章
可以根据字数或是行数进行自动分页
但是如果内容中有表格,图片等时
就会出现版面混乱
我想到的办法是
在内容中合适的位置中手动加入断点
然后由系统自动寻找断点并进行自动分页
是麻烦了点
不过是我想到的最好的办法了
大家还有什么更好的办法没啊?
讨论一下^-^-^
为了版美观,有时需要将一编较长的文章分页来显示,这时只好将文章分多次存入数据库,极不方便
本人见过多种自动分页代码,感觉上不是很理想
偶的思路是统计文章的所有行数,按指定行数输出显示内容并生成分页导航



































































http://www.sy8.net.ru/article.asp?id=340