1100万信质地产:小妹急求,这个asp站内搜索如何分页啊

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/10 11:54:57
网上看到一篇文章,试用了一下,感觉不错,可是如果数据量大了怎么分页呢,希望哪位哥哥帮着改造一下。以下是原文

第一步,我们要建立一个名为db_sample.mdb的数据库(本文以Access2000数据库为例),并在其中建立表T_Sample。表T_Sample包括如下字段:
ID 自动编号
U_Name 文本
U_Info 备注
第二步,我们开始设计搜索页面Search.asp。该页面包括一个表单(Frm_Search),表单内包括一个文本框和一个提交按钮。并将表单的method属性设为“get” ,action属性设为“Search.asp",即提交给网页自身。代码如下:
<!-- Search.asp -->
<form name="frm_Search" method="get" action="Search.asp">
请输入关键字:
<input type="text" name="key" size="10">
<input type="submit" value="搜索">
</form>
下面,就进入了实现智能搜索的关键部分。
首先,建立数据库连接。在Search.asp的开始处加入如下代码:
<%
Dim strProvider,CNN
strProvider="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
strProvider=strProvider & Server.MapPath("\") & "\data\db_Sample.mdb" '假设数据库存放在主页根目录下的data目录下
Set CNN = Server.CreateObject("ADODB.connection")
CNN.Open strProvider '打开数据库连接
%>
接下来,判断 ASP页所接收到的数据,并在数据库中进行搜索。
<%
Dim S_Key,RST,StrSQL
S_Key = Trim(Request("key")) '得到搜索关键字的值
If S_Key <>"" then
Set RST=Server.CreateObject("ADODB.RecordSet")
StrSQL=AutoKey(S_Key) '此处使用自定义函数 AutoKey(),该函数为实现智能搜索的核心
RST.Open StrSQL,CNN,3,2 '得到搜索后的记录
If RST.BOF And RST.EOF Then
%>
<font color="#FF0000">未找到任何结果!!!</font>
<%
Else
%>
搜索名称为“<font color="#FF0000"><%= S_Key %></font>”的项,共找到 <font color="#FF0000"><%= RST.RecordCount %></font> 项:<p>
<%
While Not RST.EOF '遍历整个记录集,显示搜索到的信息并设置链接
%>
<!-- 此处可设为你所需要的链接目标 -->
<font style="font: 12pt 宋体"><a href="info.asp?ID=<%= RST("ID") %>" target="_blank"><%= RST("U_Name") %></a></font><br>
<!-- 显示部分详细内容 -->
<font style="font: 9pt 宋体"><%= Left(RST("U_Info"),150) %></font><p>
<%
RST.MoveNext
Wend
RST.Close
Set RST=Nothing
End If
End If
%>

<%
Dim strProvider,CNN
strProvider="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
strProvider=strProvider & Server.MapPath("\") & "\data\db_Sample.mdb" '假设数据库存放在主页根目录下的data目录下
Set CNN = Server.CreateObject("ADODB.connection")
CNN.Open strProvider '打开数据库连接
%>
接下来,判断 ASP页所接收到的数据,并在数据库中进行搜索。
<%
Dim S_Key,RST,StrSQL
S_Key = Trim(Request("key")) '得到搜索关键字的值
If S_Key <>"" then
Set RST=Server.CreateObject("ADODB.RecordSet")
StrSQL=AutoKey(S_Key) '此处使用自定义函数 AutoKey(),该函数为实现智能搜索的核心
RST.Open StrSQL,CNN,3,2 '得到搜索后的记录
If RST.BOF And RST.EOF Then
%>
<font color="#FF0000">未找到任何结果!!!</font>
<%
response.end
page =CInt(request("page"))
if page<=0 then
page = 0
end if

for i =1 to RST.pagecount
response.write "<a href=search.asp?id="+i+">第"+i+"页</a>"
next
response.write "<BR>"
%>

搜索名称为“<font color="#FF0000"><%= S_Key %></font>”的项,共找到 <font color="#FF0000"><%= RST.RecordCount %></font> 项:<p>
<%
RST.pagerecordcount=20 '这里我忘记是什么了,设置页面大小的,查查看
RST.absolutepage = page
if page = RST.pagecount then
num = RST.recordcount mod RST.pagerecordcount
else
num = RST.pagerecordcount
end if
'NUM保存的是页面的记录数,如果是最后一页可能不是页面记录数
for i =1 to num '遍历本页记录集,显示搜索到的信息并设置链接
%>
<!-- 此处可设为你所需要的链接目标 -->
<font style="font: 12pt 宋体"><a href="info.asp?ID=<%= RST("ID") %>" target="_blank"><%= RST("U_Name") %></a></font><br>
<!-- 显示部分详细内容 -->
<font style="font: 9pt 宋体"><%= Left(RST("U_Info"),150) %></font><p>
<%
RST.MoveNext
next
RST.Close
Set RST=Nothing
End If
End If
%>

把<%
RST.MoveNext
next
RST.Close
Set RST=Nothing
End If
End If
%>
改为
<%
RST.movenext
if RST.eof then exit for
next
%>

<table width=560 border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td> </td>
</tr>
<tr bgcolor="#FFFFFF">
<form method=Post>
<td height="30" align="center">
<span class="style1">
<%if Page<2 then
response.write "上一页 首页  "
else
response.write "<a href=当前页(如search.asp)?page=1>首页</a> "
response.write "<a href=当前页(如search.asp)?page=" & Page-1 & ">上一页</a> "
end if
if RST.pagecount-page<1 then
response.write "下一页 尾页"
else
response.write "<a href=当前页(如search.asp)?page=" & (page+1) & ">"
response.write "下一页</a> <a href=当前页(如search.asp)?page="&RST.pagecount&">尾页</a>"
end if
response.write " 页次:<strong><font color=red>"&Page&"</font>/"&RST.pagecount&"</strong>页 "
response.write " 共<b><font color='#FF0000'>"&RST.recordcount&"</font></b>条记录 <b>"&RST.pagesize&"</b>条记录/页"
response.write " 转到:<input type='text' name='page' size=4 maxlength=10 class=input value="&page&">"
response.write " <input class=input type='submit' value=' Goto ' name='cndok'></span></p>"
%>
</span></td>
</form>
</tr>
</table>
<%
end if
RST.close
set RST=nothing
%>

加QQ 522343207
让哥哥教你