<%

'连接数据库

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

%>

asp怎么实现内容页分页?asp怎么实现内容页自动分页或指定分页字符后根据分页字符分页?下面发几个函数出来,大家参考下

<%

Const maxPagesize=1000     '设置分页字数

Function InsertPageBreak(strText)   '插入分页字符

Dim strPagebreak,s,ss

Dim i,IsCount,c,iCount,strTemp,Temp_String,Temp_Array

strPagebreak="[hiweb_break]"

s=strText

If Len(s)<maxPagesize Then

   InsertPageBreak=s

End If

s=Replace(s, strPagebreak, "")

s=Replace(s, "&nbsp;", "<&nbsp;>")

s=Replace(s, "&gt;", "<&gt;>")

s=Replace(s, "&lt;", "<&lt;>")

s=Replace(s, "&quot;", "<&quot;>")

s=Replace(s, "&#39;", "<&#39;>")

If s<>"" and maxPagesize<>0 and InStr(1,s,strPagebreak)=0 then

   IsCount=True

   Temp_String=""

   For i= 1 To Len(s)

    c=Mid(s,i,1)

    If c="<" Then

     IsCount=False

    ElseIf c=">" Then

     IsCount=True

    Else

     If IsCount=True Then

      If Abs(Asc(c))>255 Then

       iCount=iCount+2

      Else

       iCount=iCount+1

      End If

      If iCount>=maxPagesize And i<Len(s) Then

       strTemp=Left(s,i)

       If CheckPagination(strTemp,"table|a|b>|i>|strong|div|span") then

        Temp_String=Temp_String & Trim(CStr(i)) & ","

        iCount=0

       End If

      End If

     End If

    End If

   Next

   If Len(Temp_String)>1 Then Temp_String=Left(Temp_String,Len(Temp_String)-1)

   Temp_Array=Split(Temp_String,",")

   For i = UBound(Temp_Array) To LBound(Temp_Array) Step -1

    ss = Mid(s,Temp_Array(i)+1)

    If Len(ss) > 380 Then

     s=Left(s,Temp_Array(i)) & strPagebreak & ss

    Else

     s=Left(s,Temp_Array(i)) & ss

    End If

   Next

End If

s=Replace(s, "<&nbsp;>", "&nbsp;")

s=Replace(s, "<&gt;>", "&gt;")

s=Replace(s, "<&lt;>", "&lt;")

s=Replace(s, "<&quot;>", "&quot;")

s=Replace(s, "<&#39;>", "&#39;")

InsertPageBreak=s

End Function

Function CheckPagination(strTemp,strFind)   '检测有没包含table|a|b>|i>|strong|div|span等字符,避免分页时出错

Dim i,n,m_ingBeginNum,m_intEndNum

Dim m_strBegin,m_strEnd,FindArray

strTemp=LCase(strTemp)

strFind=LCase(strFind)

If strTemp<>"" and strFind<>"" then

   FindArray=split(strFind,"|")

   For i = 0 to Ubound(FindArray)

    m_strBegin="<"&FindArray(i)

    m_strEnd   ="</"&FindArray(i)

    n=0

    do while instr(n+1,strTemp,m_strBegin)<>0

     n=instr(n+1,strTemp,m_strBegin)

     m_ingBeginNum=m_ingBeginNum+1

    Loop

    n=0

    do while instr(n+1,strTemp,m_strEnd)<>0

     n=instr(n+1,strTemp,m_strEnd)

     m_intEndNum=m_intEndNum+1

    Loop

    If m_intEndNum=m_ingBeginNum then

     CheckPagination=True

    Else

     CheckPagination=False

     Exit Function

    End If

   Next

Else

   CheckPagination=False

End If

End Function

Function ContentPagination(hiwebstr)   '内容分页

Dim ContentLen, maxperpage, Paginate

Dim arrContent, strContent, i

Dim m_strFileUrl,m_strFileExt,ArticleID

ArticleID=Request.QueryString("ID")

strContent = InsertPageBreak(hiwebstr)

ContentLen = Len(strContent)

CurrentPage=Request.QueryString("Page")

If CurrentPage="" Then CurrentPage=0

If InStr(strContent, "[hiweb_break]") <= 0 Then

   ArticleContent = "<div id=""NewsContentLabel"" class=""NewsContent"">" & strContent & "</div><div id=""Message"" class=""Message""></div>"

Else

   arrContent = Split(strContent, "[hiweb_break]")

   Paginate = UBound(arrContent) + 1

   If CurrentPage = 0 Then

    CurrentPage = 1

   Else

    CurrentPage = CLng(CurrentPage)

   End If

   If CurrentPage < 1 Then CurrentPage = 1

   If CurrentPage > Paginate Then CurrentPage = Paginate

   strContent = "<div id=""NewsContentLabel"" class=""NewsContent"">"& arrContent(CurrentPage - 1)

   ArticleContent = ArticleContent & strContent

   If UserArticle = True Then

    ArticleContent = ArticleContent & "</p></div><div id=""Message"" class=""Message""></div><p align=""center""><b>"

   Else

    ArticleContent = ArticleContent & "</p></div><p align=""center""><b>"

   End If

   If IsURLRewrite Then

    m_strFileUrl = ArticleID & "_"

   Else

    m_strFileExt = ""

    m_strFileUrl = "?id=" & ArticleID & "&Page="

   End If

   If CurrentPage > 1 Then

    If IsURLRewrite And (CurrentPage-1) = 1 Then

     ArticleContent = ArticleContent & "<a href="""& ArticleID & m_strFileExt & """>上一页</a>&nbsp;&nbsp;"

    Else

     ArticleContent = ArticleContent & "<a href="""& m_strFileUrl & CurrentPage - 1 & m_strFileExt & """>上一页</a>&nbsp;&nbsp;"

    End If

   End If

   For i = 1 To Paginate

    If i = CurrentPage Then

     ArticleContent = ArticleContent & "<font color=""red"">[" & CStr(i) & "]</font>&nbsp;"

    Else

     If IsURLRewrite And i = 1 Then

      ArticleContent = ArticleContent & "<a href="""& ArticleID & m_strFileExt & """>[" & i & "]</a>&nbsp;"

     Else

      ArticleContent = ArticleContent & "<a href="""& m_strFileUrl & i & m_strFileExt & """>[" & i & "]</a>&nbsp;"

     End if

    End If

   Next

   If CurrentPage < Paginate Then

    ArticleContent = ArticleContent & "&nbsp;<a href="""& m_strFileUrl & CurrentPage + 1 & m_strFileExt & """>下一页</a>"

   End If

   ArticleContent = ArticleContent & "</b></p>"

End If

Response.Write(ArticleContent)

End Function

%>


1.把上面的函数放在一个文件中,在需要分页的页面最顶部调用这个文件。调用代码 <!--include file="***.asp"-->

2.在内容显示处 将原来的显示代码替换为: <%=ContentPagination(您以前的内容显示变量)%>。

 

  1. 1