·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP网站建设 >> ASP分页函数、分页导航

ASP分页函数、分页导航

作者:佚名      ASP网站建设编辑:admin      更新时间:2022-07-23
程序代码
<%
'//分页函数
    '//psize:每页显示的数据数
    '//pindex:当前页码
    '//tbName:表名
    '//keyIndex:根据什么字段分页,一般是自增长类型(access数据库的自动编号)
    '//where:查询条件
    '//order:排序条件,缺省为" order by " &keyIndex &" desc"
    '//总记录数与总页数可从返回的结果集中直接获取,字段datacount存储总记录数,字段pagecount存储总页数
    PRivate pindex,datacount,pages
    'datacount = 0
    'pages = 1
    public function changePage(psize,tbName,keyIndex,where,order)
        dim sqlstring
        pindex = Trim(Request.QueryString("page"))
        if not isnumeric(psize) or psize="" then psize=1'//每页显示的数据数
        if not isnumeric(pindex) or pindex="" then pindex=1'//当前页码
        if order="" then order=" order by " & keyIndex & " desc"
        '//获取总数据数
        'dim datacount,pages
        set rs=conn.execute("select count(*) as datacount from " & tbName & " where 1=1 " & where)
        datacount = rs("datacount")'//总记录数
        rs.close
        set rs=nothing
        '//计算总页数
        if (datacount mod psize)=0 then
            pages=datacount \ psize
        else
            pages=datacount \ psize + 1
        end if
        '//
        if cint(pindex)>pages then pindex=pages
        '拼接sql字符串
        if pindex<=1 then
            sqlstring="select top " & psize & " *," & datacount & " as datacount," & pages & " as pagecount from " &_
                        tbName & " where 1=1 " & where & " " & order
        else
            sqlstring="select top " & psize & " *," & datacount & " as datacount," & pages & " as pagecount from " &_
                        tbName & " where 1=1 and " & keyIndex & " not in(select top " & (pindex-1)*psize & " " & keyIndex & " from " &_
                        tbName & " where 1=1 " & where & " " & order & ") " & where & " " & order
        end if
        'Response.Write(sqlstring)
        set changePage=conn.execute(sqlstring)
    end function
    '//分页导航
    '//fileName:文件名/当前页面的话,可以留空
    '//argString:分页参数例如classid=1&tid=16,分页必须的参数page不必填写
    '//pindex:当前页码
    '//datacount:总记录数
    '//pages:总页数
    '//showMsg:是否显示分页信息,参数为true/false
    '//showText:是否显示首页、上页、下页、末页的导航,参数为true/false
    '//showNumber:是否显示数字分页导航,参数为true/false
    'public function pageLink(fileName,argString,pindex,datacount,pages,showMsg,showText,showNumber)
    public function pageLink(fileName,argString,showMsg,showText,showNumber)
        '//
        if argString<>"" then argString = argString & "&"
        if not showText and not showNumber then showText=true
        '//
        if showMsg then
            Response.Write("[")
            Response.Write("第 <span style='color:red;'>" & pindex & "</span> 页")
            Response.Write("/分 <span style='color:red;'>" & pages & "</span> 页")
            Response.Write("/总 <span style='color:red;'>" & datacount & "</span> 条记录")
            Response.Write("] ")
        end if
        '//
        if showText then
            if pindex>1 then
                Response.Write("<a href='" & fileName & "?" & argString & "page=1'>[首页]</a>")
                Response.Write(" ")
                Response.Write("<a href='" & fileName & "?" & argString & "page=" & pindex - 1 & "'>[上页]</a>")
            else
                Response.Write("[首页]")
                Response.Write(" ")
                Response.Write("[上页]")
            end if
            Response.Write(" ")
            if pindex<pages then
                Response.Write("<a href='" & fileName & "?" & argString & "page=" & pindex + 1 & "'>[下页]</a>")
                Response.Write(" ")
                Response.Write("<a href='" & fileName & "?" & argString & "page=" & pages & "'>[末页]</a>")
            else
                Response.Write("[下页]")
                Response.Write(" ")
                Response.Write("[末页]")
            end if
        end if
        '//
        if showNumber then
            Response.Write("      ")
            for i = 4 to 1 step -1
                if (pindex - i)>0 then
                    Response.Write("<a href='" & fileName & "?" & argString & "page=" & pindex - i & "'>" & pindex - i & "</a>")
                    Response.Write(" ")
                end if
            next
            '//
            Response.Write("<span style='color:red;'>" & pindex & "</span>")
            '//
            for i = 1 to 4
                if (pindex + i)<=pages then
                    Response.Write(" ")
                    Response.Write("<a href='" & fileName & "?" & argString & "page=" & pindex + i & "'>" & pindex + i & "</a>")
                end if
            next
            '//
        end if
        '//
    end function
%>