星期零

技术改变生活,分享让我们快乐!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

asp入门之分页

Posted on 2010-08-13 16:19  weekzero  阅读(394)  评论(0编辑  收藏  举报

分页是十分重要的功能,到处都会用到

asp分页比较简单,下面一个简单的例子

 

代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    
<title>公司</title>
    
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>

<table width="500" border="0" align="center" cellpadding="2" cellspacing="0">
<%
    
dim conn,connstr
    
    
Set conn = Server.CreateObject("ADODB.Connection")
    connstr
="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("d/d.mdb")
    conn.Open connstr
    
    
set rs=server.CreateObject("adodb.recordset")
    rs.Open 
"select * from news order by createdate desc",conn,1,1
    
if not rs.EOF then
    
        rs.PageSize
=10
        page
= clng(Request("page"))
        
if page=""or page<1 then page=1
        
if page>rs.PageCount then page=rs.PageCount
        rs.AbsolutePage
=page
        
        pagec
=rs.PageCount
        pagecurrent
=page
        
        
for i=1 to rs.PageSize
%>
    
<tr>
        
<td width="50" height="25" align="center">
            
<%=rs("sid")%>
        
</td>
        
<td width="450">
            
<href="shownews.asp?id=<%=rs("sid")%>">
                
<%=rs("title")%></a>
        
</td>
        
<td width="100" align="center">
            
<%=rs("createdate")%>
        
</td>
    
</tr>
<%
            rs.MoveNext
            
if rs.EOF then exit for
        
next

    
else 
        response.Write(
"<tr><td align='center'>暂无记录</td></tr>")
    
end if
%>
</table>
<table width="500" border="0" align="center">
  
<tr> 
    
<td height="30" align="right"> 
      
<%    
        
if page<>1 then
            response.Write(
"【<a href=l.asp?page=1>首页</a>】")
            response.Write(
"【<a href=l.asp?page="&(page-1)&">上一页</a>】")
        
end if
        
if page<>rs.pagecount then
            response.Write(
"【<a href=l.asp?page="&(page+1)&">下一页</a>】")
            response.Write(
"【<a href=l.asp?page="&rs.pagecount&">尾页</a>】")
        
end if
        
%>
      [
&nbsp;<font color="#cc3300"><%= page %></font>&nbsp;/&nbsp;<font color="#cc3300"><%= rs.pagecount %></font>&nbsp;]</td>
    
<td width="3%"></td>
  
</tr>
</table>
<%
    rs.close
    
set rs = nothing
%>                                    
</body>
</html>

 

效果如下

 

 

另外一个封装了的分页function,很好用,代码如下

 

代码
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
.pagenum 
{ margin:2px 2px 2px 2px;display:inline-block;border:#95C0D0 1px solid; padding:1px 7px 0px 7px; line-height:21px; font-size:14px;}
.pagecur
{ margin:2px 2px 2px 2px;display:inline-block;border:#95C0D0 1px solid;background-color:#A8D0C0;font-weight:bold; font-size:14px; line-height:21px; color:Red; padding:0px 5px 0px 5px;}
-->
</style>
<%
    
function wzdataset(WzDsRs,WzPageSize,linkStr,wztablewidth)
        
%>
        
<TABLE width="<%=wztablewidth%>" border="1" cellpadding="0" cellspacing="0" style="border-collapse:collapse;">
        
<%
        
if WzDsRs.eof then
        
%>
        
<TR align="center">
            
<TD height="50">暂时无信息</TD>
        
</TR>
        
<%
        
else
            WzDsRs.PageSize
=WzPageSize
            WzDsRs_Page
= clng(Request("page"))
            
if WzDsRs_Page=""or WzDsRs_Page<1 then WzDsRs_Page=1
            
if WzDsRs_Page>WzDsRs.PageCount then WzDsRs_Page=WzDsRs.PageCount
            WzDsRs.AbsolutePage
=WzDsRs_Page
            
        
%>
        
<tr height="25">
        
<%
            
for WzDsRsTd_i=0 to WzDsRs.Fields.count-1
        
%>
            
<td nowrap align="center"><b><%=WzDsRs.fields(WzDsRsTd_i).name%></b></td>
        
<%
            
next
        
%>
        
</tr>
        
<%
            
for WzDsRsTr_i=1 to WzDsRs.PageSize                
            
%>
            
<TR onMouseOver="ItemOver(this)"<%if WzDsRsTr_i mod 2 = 0 then%> style="background-color:#F2F2F2 "<%end if%>>
                
<%
                    
for WzDsRsTd_i=0 to WzDsRs.Fields.count-1
                
%>
                
<TD height="25" align="center"><%=WzDsRs.fields(WzDsRsTd_i).value%></TD>
                
<%
                    
next
                
%>
            
</TR>
            
<%
                WzDsRs.MoveNext
                
if WzDsRs.EOF then exit for            
            
next
                
if WzDsRs.pagecount>1 then
                    
%>
                    
<TR align="center">
                        
<TD colspan="<%=WzDsRs.Fields.count%>">
                            
<table width="98%" border="0">
                                
<tr> 
                                 
<td height="50" align="center">
                                  
<%
                                 
dim wzleft,wzright,wzpagecount,wzcurrentpage
                                 wzcurrentpage
= WzDsRs_Page
                                 wzpagecount
=WzDsRs.pagecount
                                 
if len(wzcurrentpage)<2 then
                                     wzleft
=0
                                 
else
                                     wzleft
=left(wzcurrentpage,len(wzcurrentpage)-1)
                                 
end if
                                 wzright
=right(wzcurrentpage,1)
                                 
if wzright=0 then
                                     wzleft
=cint(wzleft)-1
                                 
end if
                                 
                                 
if cint(wzcurrentpage)<>1 then
                                     response.Write(
"<a class=pagenum title=首页 href="&linkStr&"&page=1> << </a>")
                                 
end if
                                                 
                                 
if WzDsRs_Page>10 then
                                     response.Write(
"<a class=pagenum title=上十页 href="&linkStr&"&page="&wzleft*10&"> < </a>&nbsp;")
                                 
end if
                                 
                                 
for i=1 to 10                                
                                         
if  wzleft*10+i=WzDsRs_Page then        
                                             response.Write(
"<font class=pagecur>"&wzleft*10+i&"</font>")
                                        
else
                                            response.Write(
"<a class=pagenum href="&linkStr&"&page="&wzleft*10+i&">"&wzleft*10+i&"</a>")
                                        
end if
                                        
if wzleft*10+i=wzpagecount then exit for
                                 
next
                                 
                                 
dim wzpagecountleft
                                 wzpagecountleft
=left(wzpagecount,len(wzpagecount)-1)
                                 
if right(wzpagecount,1)=0 then
                                     wzpagecountleft
=cint(wzpagecountleft)-1
                                 
end if
                                 
if wzpagecountleft="" then
                                     wzpagecountleft
=0
                                 
end if
                                 
if cint(wzleft)<cint(wzpagecountleft) then        
                                     response.Write(
"<a class=pagenum title=下十页 href="&linkStr&"&page="&wzleft*10+11&"> > </a>")
                                 
end if
                                 
                                 
if wzcurrentpage<>wzpagecount then
                                      response.Write(
"<a class=pagenum title=尾页 href="&linkStr&"&page="&wzpagecount&"> >> </a>")
                                  
end if
                                  
%>
                                    
<span class=pagenum><%=WzDsRs_Page%>/<%=WzDsRs.pagecount%>】 【总记录:<%=WzDsRs.recordcount%></span>
                                    
</td>
                                
</tr>
                            
</table>
                        
</TD>
                    
</TR>
                    
<%
                
end if
        
end if
        
%>
</TABLE>
<%
    
end function
%>

<script language="javascript" type="text/javascript">    
    
// 鼠标经过改变行的颜色
    if (!objbeforeItem)
    {
        
var objbeforeItem=null;
        
var objbeforeItembackgroundColor=null;
    }    
    
function ItemOver(obj)
    {
        
if(objbeforeItem)
        {
            objbeforeItem.style.backgroundColor 
= objbeforeItembackgroundColor;
        }
        objbeforeItembackgroundColor 
= obj.style.backgroundColor;
        objbeforeItem 
= obj;
        obj.style.backgroundColor 
= "#B9D1F3";     
    }
    
// 
</script>

 

调用代码:

 

代码
<!--#include file="wzdataset.asp"-->
        
<%
            
set rs=server.CreateObject("adodb.recordset")
            rs.Open 
"select sid as 编号,title as 标题,createdate as 建立时间 from news order by createdate desc",conn,1,1
            
if not rs.EOF then
            
                wzdataset rs,
11,"list.asp?1=1",800
            
end if
        
%>

 

wzdataset.asp文件是function的文件

 

效果如下: