执行特殊自定义asp过滤条件后如何对记录集分页
asp取出recordset后,如果需要执行特殊自定义的asp过滤语句,满足条件后再输出对应的记录,而这个asp过滤条件无法融合到sql语句中时,此时asp要如何对记录集进行正确的分页操作?
要对需要执行特殊自定义asp过滤的记录集进行分页操作,不能使用asp提供的默认分页操作,如rs.pagesize,rs.recordcount,rs.pagecount,rs.absolutepage这种asp的记录集分页属性,需要自己编写分页逻辑。
分页思路:取出所有的记录,先执行遍历操作,计算满足asp过滤的条件的总记录数,然后对记录数进行分页计算,依据page参数得到要取的实际数据的范围,重置asp记录集recordset的位置到第一位,再次遍历recordset获取满足条件的记录。
分页模型如下,注意修改文字描述部分的条件
<%set rs= Server.CreateObject("adodb.recordset") sql="你的sql语句,需要选出所有的记录" rs.open sql,conn,1,1 %> <table> <tr>td表头</tr> <% if rs.eof or rs.bof then%> <tr>一个td容器提示没有信息,注意依据表头td个数设置colspan属性</tr> <% Else'有记录 total=0'总记录数 while not rs.oef '计算总的符合要求的数据量 if 你的asp过滤条件 then total=total+1'满足+1总记录数 rs.movenext wend if total=0 then'没有符合asp过滤后的记录 %> <tr>一个td容器提示没有信息,注意依据表头td个数设置colspan属性</tr> <% else'有满足条件的记录 rs.movefirst'移动到第一条,以便取数据 pagesize=30'定义每页显示多少条记录 '计算实际的总页数 if total mod pagesize =0 then Pagecount=total\pagesize else Pagecount=total\pagesize+1 end if Page=int(Request.QueryString("page"))'获取当前页参数 if page <=0 then page=1 if page>pagecount then page=1 index=0'记录已经取到的满足asp过滤条件的记录下标 startindex=0'记录分页后符合条件的开始记录下标 endindex=0'记录分页后符合条件的结束记录下标 '计算开始(>=)和结束(<)下标 startindex=(page-1)*pagesize+1 endindex=startindex+pagesize do while not rs.eof'再次遍历取数据 if 你的asp过滤条件 then'满足条件 index=index+1 if index>=startindex and index<endindex then'在分页范围内的下标 %> <tr>输出td数据</tr> <% end if if index>=endindex then exit do'大于等于要去的记录最大下标退出循环 end if rs.movenext loop end if end if%> </table> <!--分页导航输出--> 共<%=total%>条记录/每页<%= pagesize %>条记录,当前第<%=page%>页/共<%=pagecount %>页 <a <%if page=1 then%>onclick="javascript:alert('这已经是第一页了');" href="javascript:void(0)" <%else%>href="?page=1" <%end if%>>首 页</a> <a <%if page=1 then%>onclick="javascript:alert('没有上一页了');" href="javascript:void(0)" <%else%>href="?page=<%=page-1%>" <%end if%>>上一页</a> <a <%if page=pagecount then%>onclick="javascript:alert('这已经是最后一页了');" href="javascript:void(0)" <%else%>href="?page=<%=page+1%>" <%end if%>>下一页</a> <a <%if page=pagecount then%>onclick="javascript:alert('这已经是最后一页了');" href="javascript:void(0)" <%else%>href="?page=<%=pagecount%>" <%end if%> >尾 页</a> 转到第<select name="select" id="select1" onChange="window.location=this.value"> <% for e=1 to pagecount%> <option value="?page=<%=e%>" <%if e=page then response.Write("selected") end if%> ><%=e%> </option> <% next%></select>页
加支付宝好友偷能量挖...
原创文章,转载请注明出处:执行特殊自定义asp过滤条件后如何对记录集分页