ASP分页函数&&ASP分頁導航

    '//分页函数
    '//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(
"]&nbsp;")
        
end if
        
'//
        if showText then
            
if pindex>1 then
                Response.Write(
"<a href='" & fileName & "?" & argString & "page=1'>[首页]</a>")
                Response.Write(
"&nbsp;")
                Response.Write(
"<a href='" & fileName & "?" & argString & "page=" & pindex - 1 & "'>[上页]</a>")
            
else
                Response.Write(
"[首页]")
                Response.Write(
"&nbsp;")
                Response.Write(
"[上页]")
            
end if
            Response.Write(
"&nbsp;")
            
if pindex<pages then
                Response.Write(
"<a href='" & fileName & "?" & argString & "page=" & pindex + 1 & "'>[下页]</a>")
                Response.Write(
"&nbsp;")
                Response.Write(
"<a href='" & fileName & "?" & argString & "page=" & pages & "'>[末页]</a>")
            
else
                Response.Write(
"[下页]")
                Response.Write(
"&nbsp;")
                Response.Write(
"[末页]")
            
end if
        
end if
        
'//
        if showNumber then
            Response.Write(
"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")
            
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(
"&nbsp;")
                
end if
            
next
            
'//
            Response.Write("<span style='color:red;'>" & pindex & "</span>")
            
'//
            for i = 1 to 4
                
if (pindex + i)<=pages then
                    Response.Write(
"&nbsp;")
                    Response.Write(
"<a href='" & fileName & "?" & argString & "page=" & pindex + i & "'>" & pindex + i & "</a>")
                
end if
            
next
            
'//
        end if
        
'//
    end function
posted @ 2009-05-15 15:34  Athrun  阅读(561)  评论(0编辑  收藏  举报