在同一页面中如何实现查询并分页显示结果

<p><!--#includefile="..\conn.asp"--><%key=request("key")type1=request("type")ifkey=""... <p> <!--#include file="..\conn.asp"-->
<%
key=request("key")
type1=request("type")
if key="" then
'response.write "<script>alert('查找字符串不能为空!');history.back();</'script>"
response.Write "<script>alert('查找字符不能为空,请重输入查找的信息')<a href=""javascript:history.go(-1)"">返回重查 </script></a>"
'call error()
response.end
end if
%>
</p>
<table width="700" border="1" align="center" cellpadding="2" cellspacing="0" bordercolor="#000000" bgcolor="#EEEEEE" style="border-collapse: collapse; border-style: dotted; border-width: 0px">

<%
page=clng(request("page"))

if type1="name" then
sql="select * from travelspot where travelspot like '%"&key&"%'"
elseif type1="city" then
sql="select * from travelspot where city like '%"&key&"%'"
end if
rs.open sql,conn,1,3
if rs.eof and rs.bof then
response.write "<tr bgcolor='#ffffff'><td colspan='4'><p align='center'>对不起,没有找到相关信息</p></td></tr>"
else
%>
<tr bgcolor="#00ccff">
<td align="center" bgcolor="#00ccff" class="STYLE1" >景点名称</td>
<td bgcolor="#00ccff" class="STYLE1"> <div align="center">景点所在城市</div></td>
<td bgcolor="#00ccff" class="STYLE1"><div align="center">景点介绍</div></td>
<td bgcolor="#00ccff" class="STYLE1"><div align="center">景点图片</div></td>
</tr>
<%
rs.pagesize=8
if page=0 then page=1
pages=rs.pagecount
if page > pages then page=pages

for j=1 to rs.pagesize
if rs.eof or rs.bof then exit for
%>
<tr class="STYLE3">
<td align="center"><%=rs("travelspot")%></td>
<td align="center" ><%=rs("city")%></td>
<td align="center"><%=rs("introduce")%></td>
<td align="center"><img src="<%=rs("picture")%>" width="40" height="40"></td>
</tr>
<%
rs.movenext

next
%>
<tr class="STYLE3">
<td height="24" colspan="4" align="center">
<div align="center">
<%if page<2 then
response.write "首页 上一页 "
else
response.write "<a href=searchspot.asp.asp?page=1>首页</a> "
response.write "<a href=searchspot.asp?page=" & page-1 & ">上一页</a> "
end if
if rs.pagecount-page<1 then
response.write "下一页 尾页"
else
response.write "<a href=searchspot.asp?page=" & (page+1) & ">"
response.write "下一页</a> <a href=searchspot.asp?page="&rs.pagecount&">尾页</a>"
end if
response.write " 页次:<strong><font color=red>"&page&"</font>/"&rs.pagecount&"</strong>页 "
response.write " 共<b><font color='#ff0000'>"&rs.recordcount&"</font></b>条记录 <b>"&rs.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>"
%>
</div></td>
</tr>
</table>

<%
end if
rs.close
set rs=nothing
conn.close()
%>
展开
 我来答
roobie
推荐于2016-04-01 · TA获得超过1387个赞
知道小有建树答主
回答量:947
采纳率:0%
帮助的人:819万
展开全部
建立access的数据库news,还有表news,表的字段(id,title),id唯一,输入数据保存,用下面代码可查询,可分页
-----------------------下面保存为search.asp--------------------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>文件</title>
</head>
<body bgcolor="#ffffff">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<script>
function btn_ck_bh_Click()
{
var cx = document.form1.cxsj.value;
form1.action ="search.asp?cx="+cx;
}
</script>
<table border="1" cellspacing="0" bgcolor="#F0F8FF" bordercolorlight="#4DA6FF" bordercolordark="#ECF5FF" width="88%" style="word-break:break-all">
<tr>
<td width="778" align="center" colspan="7">
<form method="POST" name="form1" action=search.asp>
<p>输入搜索内容:<input type="text" name="cxsj" size="20"><input type="submit" value="提交" name="B1" LANGUAGE="javascript" onclick="btn_ck_bh_Click()">
<input type="reset" value="重写" name="B2"></p>
</form>
</td>
</tr>
</table>
<table border="1" cellspacing="0" bgcolor="#F0F8FF" bordercolorlight="#4DA6FF" bordercolordark="#ECF5FF" width="88%" style="word-break:break-all">
<tr>
<td width="8%" align="center"><strong><font color="#0080C0">ID 号</font></strong></td>
<td width="58%" align="center"><strong><font color="#0080C0">标 题</font></strong></td>
<td width="8%" align="center"><strong><font color="#0080C0">修 改</font></strong></td>
<td width="8%" align="center"><strong><font color="#0080C0">删 除</font></strong></td>
</tr>
<%
'数据库查询
'获得搜索内容
cx = request("cx")
dim pageCount
'把page转换成整数
page = cint(request("page"))
set conn=server.createobject("adodb.connection")'
set rs=server.createobject("adodb.recordset")
conn.open "DBQ=" & server.mappath("./news.mdb") & ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"

' 获取产品的名字记录集(从 news表中)
if cx <> "" then
sql = "select * from news where title like '%"&cx& "%' order by id desc"
else
sql ="select * from news order by id desc"
end if
rs.open sql,conn,3,3
'如果没有数据记录
if rs.bof then
errmsg=errmsg+"<br>"+"<li>"+keyword+"没有记录,请返回!!"
response.write errmsg
response.end
end if

' 设置记录集在每页的总行数,也就是 PageSize属性
RS.PageSize=40
'把rs.pageCount转换成整数和page才能作比较
pageCount = cint(rs.pageCount)
' 设置当前的页号( AbsolutePage属性)
if page = 0 then
page =1
end if
RS.AbsolutePage = page
x=1
' 显示当前页中的所有记录( PageSize中设置的行数)

WHILE NOT RS.EOF AND NumRows<RS.PageSize
%>
<tr onmouseover="this.bgColor='#99ccff'" onmouseout="this.bgColor=''">
<td width="8%"><p align="center"><%=rs("id")%></td>
<td width="58%"><a href="view.asp?id=<%=rs("id")%>" target="_blank"><%=rs("title")%></a></td>
<td width="8%" align="center"><a href="edit.asp?id="<%=rs("id")%>>修 改</a></td>
<td width="8%" align="center"><a href="delet.asp?id="<%=rs("id")%>>删 除</a></td>
</tr>
<%RS.MoveNext
NumRows=NumRows+1
WEND%>
<tr onmouseover="this.bgColor='#99ccff'" onmouseout="this.bgColor=''">
<td width="105%" align="center" colspan="6"> </td> </tr>
<tr>
<td width="105%" align="center" colspan="6">
<p align="center"><FONT color=#333333>共<%=PageCount%>页 第<%=page%>页★
<%if page=1 then%>首页<%end if%>
<%if page>1 then%>
<A HREF="search.asp?page=1&cx=<%=cx%>"> 首页</A>
<%end if%>★
<%if page>1 then%><A HREF="search.asp?page=<%=page-1%>&cx=<%=cx%>"><%end if%>上一页</a>
<%
dim pagewhere
dim p
p = 1
'把pagewhere转换成整数
'pagewhere = cint(request("pagewhere"))
pagewhere = pageCount
if pagewhere>0 then
for p=1 to pagewhere
if p <> page then%>
<A HREF="search.asp?page=<%=p%>&cx=<%=cx%>"><%=p%></a>
<%end if
if p =page then%>
<%=p%>
<% end if
next
end if%>
<%if page < PageCount then%>
<A HREF="search.asp?page=<%=page+1%>&cx=<%=cx%>">
<%end if %>下一页</A>★
<%if page=PageCount then%>尾页
<%end if%>
<%if page<PageCount then%>
<A HREF="search.asp?page=<%=PageCount%>&cx=<%=cx%>"> 尾页</A>
<%end if%>
</p></FONT></td> </tr> <tr>
<td width="105%" align="center" colspan="6">搜索内容:<%=cx%></td>
</tr>
</table></center></div>
</body></html>
<%
rs.close
Set rs=nothing
conn.close
set conn=nothing
%>
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式