dreamweavermx已经正式发布了,deamweaver4 + deamweaver ultradev 4 的组合使他成为当然的制做网页的首选工具,(好象做广告:) )
好了,进入正题,
我在以前做网页的分页时候都是用自己写的服务端脚本(我从不用ado的分页),用了mx后发现在这里面用分页太方便了,不过代码也有点太长了,大家看下面的代码就可以知道。用过之后我发现里面recordset 的cursortype设为0分页竟然可以正常工作!这令我吃惊不少,分析了代码之后才发现mx 是用了一种挺笨的方法实现的,效率很低,所以大家还是用1吧:)
分析如下:
<%@language="vbscript" codepage="936"%>
<!–#include file="connections/ncarcnn.asp" –>
<%
dim recordset1
dim recordset1_numrows
set recordset1 = server.createobject("adodb.recordset")
recordset1.activeconnection = mm_ncarcnn_string
recordset1.source = "select * from dbo.ncarinfo"
recordset1.cursortype = 0
这里用0也可以正常运行,但是经过分析代码可以看出,用0的效率很低,建议用1
recordset1.cursorlocation = 2
recordset1.locktype = 1
recordset1.open()
recordset1_numrows = 0
%>
<%
以下为分页代码
dim repeat1__numrows
dim repeat1__index
repeat1__numrows = 10
repeat1__index = 0
recordset1_numrows = recordset1_numrows + repeat1__numrows
%>
<%
*** recordset状态, 定义状态变量
dim recordset1_total
dim recordset1_first
dim recordset1_last
set the record count
recordset1_total = recordset1.recordcount
set the number of rows displayed on this page
if (recordset1_numrows < 0) then
recordset1_numrows = recordset1_total
elseif (recordset1_numrows = 0) then
recordset1_numrows = 1
end if
set the first and last displayed record
recordset1_first = 1
recordset1_last = recordset1_first + recordset1_numrows – 1
if we have the correct record count, check the other stats 处理正确的rs
if (recordset1_total <> -1) then
if (recordset1_first > recordset1_total) then
recordset1_first = recordset1_total
end if
if (recordset1_last > recordset1_total) then
recordset1_last = recordset1_total
end if
if (recordset1_numrows > recordset1_total) then
recordset1_numrows = recordset1_total
end if
end if
%>
<%
*** recordset stats: if we dont know the record count, manually count them处理错误的rs
if (recordset1_total = -1) then
count the total records by iterating through the recordset
recordset1_total=0
while (not recordset1.eof)
recordset1_total = recordset1_total + 1
recordset1.movenext
wend
reset the cursor to the beginning
if (recordset1.cursortype > 0) then
recordset1.movefirst
else
recordset1.requery
end if
set the number of rows displayed on this page
if (recordset1_numrows < 0 or recordset1_numrows > recordset1_total) then
recordset1_numrows = recordset1_total
end if
set the first and last displayed record
recordset1_first = 1
recordset1_last = recordset1_first + recordset1_numrows – 1
if (recordset1_first > recordset1_total) then
recordset1_first = recordset1_total
end if
if (recordset1_last > recordset1_total) then
recordset1_last = recordset1_total
end if
end if
%>
<%
dim mm_paramname
%>
<%
*** move to record and go to record: declare variables
dim mm_rs
dim mm_rscount
dim mm_size
dim mm_uniquecol
dim mm_offset
dim mm_attotal
dim mm_paramisdefined
dim mm_param
dim mm_index
set mm_rs = recordset1
mm_rscount = recordset1_total
mm_size = recordset1_numrows
mm_uniquecol = ""
mm_paramname = ""
mm_offset = 0
mm_attotal = false
mm_paramisdefined = false
if (mm_paramname <> "") then
mm_paramisdefined = (request.querystring(mm_paramname) <> "")
end if
%>
<%
*** move to record: handle index or offset parameter
if (not mm_paramisdefined and mm_rscount <> 0) then
use index parameter if defined, otherwise use offset parameter
mm_param = request.querystring("index")
if (mm_param = "") then
mm_param = request.querystring("offset")
end if
if (mm_param <> "") then
mm_offset = int(mm_param)
end if
if we have a record count, check if we are past the end of the recordset
if (mm_rscount <> -1) then
if (mm_offset >= mm_rscount or mm_offset = -1) then past end or move last
if ((mm_rscount mod mm_size) > 0) then last page not a full repeat region
mm_offset = mm_rscount – (mm_rscount mod mm_size)
else
mm_offset = mm_rscount – mm_size
end if
end if
end if
move the cursor to the selected record
mm_index = 0
while ((not mm_rs.eof) and (mm_index < mm_offset or mm_offset = -1))
mm_rs.movenext
mm_index = mm_index + 1
wend
if (mm_rs.eof) then
mm_offset = mm_index set mm_offset to the last possible record
end if
end if
%>
<%
*** move to record: if we dont know the record count, check the display range
if (mm_rscount = -1) then
walk to the end of the display range for this page
mm_index = mm_offset
while (not mm_rs.eof and (mm_size < 0 or mm_index < mm_offset + mm_size))
mm_rs.movenext
mm_index = mm_index + 1
wend
if we walked off the end of the recordset, set mm_rscount and mm_size
if (mm_rs.eof) then
mm_rscount = mm_index
if (mm_size < 0 or mm_size > mm_rscount) then
mm_size = mm_rscount
end if
end if
if we walked off the end, set the offset based on page size
if (mm_rs.eof and not mm_paramisdefined) then
if (mm_offset > mm_rscount – mm_size or mm_offset = -1) then
if ((mm_rscount mod mm_size) > 0) then
mm_offset = mm_rscount – (mm_rscount mod mm_size)
else
mm_offset = mm_rscount – mm_size
end if
end if
end if
reset the cursor to the beginning
if (mm_rs.cursortype > 0) then
mm_rs.movefirst
else
mm_rs.requery
end if
move the cursor to the selected record
mm_index = 0
while (not mm_rs.eof and mm_index < mm_offset)
mm_rs.movenext
mm_index = mm_index + 1
wend
end if
%>
<%
*** move to record: update recordset stats
set the first and last displayed record
recordset1_first = mm_offset + 1
recordset1_last = mm_offset + mm_size
if (mm_rscount <> -1) then
if (recordset1_first > mm_rscount) then
recordset1_first = mm_rscount
end if
if (recordset1_last > mm_rscount) then
recordset1_last = mm_rscount
end if
end if
set the boolean used by hide region to check if we are on the last record
mm_attotal = (mm_rscount <> -1 and mm_offset + mm_size >= mm_rscount)
%>
<%
*** go to record and move to record: create strings for maintaining url and form parameters
dim mm_keepnone
dim mm_keepurl
dim mm_keepform
dim mm_keepboth
dim mm_removelist
dim mm_item
dim mm_nextitem
create the list of parameters which should not be maintained
mm_removelist = "&index="
if (mm_paramname <> "") then
mm_removelist = mm_removelist & "&" & mm_paramname & "="
end if
mm_keepurl=""
mm_keepform=""
mm_keepboth=""
mm_keepnone=""
add the url parameters to the mm_keepurl string
for each mm_item in request.querystring
mm_nextitem = "&" & mm_item & "="
if (instr(1,mm_removelist,mm_nextitem,1) = 0) then
mm_keepurl = mm_keepurl & mm_nextitem & server.urlencode(request.querystring(mm_item))
end if
next
add the form variables to the mm_keepform string
for each mm_item in request.form
mm_nextitem = "&" & mm_item & "="
if (instr(1,mm_removelist,mm_nextitem,1) = 0) then
mm_keepform = mm_keepform & mm_nextitem & server.urlencode(request.form(mm_item))
end if
next
create the form + url string and remove the intial & from each of the strings
mm_keepboth = mm_keepurl & mm_keepform
if (mm_keepboth <> "") then
mm_keepboth = right(mm_keepboth, len(mm_keepboth) – 1)
end if
if (mm_keepurl <> "") then
mm_keepurl = right(mm_keepurl, len(mm_keepurl) – 1)
end if
if (mm_keepform <> "") then
mm_keepform = right(mm_keepform, len(mm_keepform) – 1)
end if
a utility function used for adding additional parameters to these strings
function mm_joinchar(firstitem)
if (firstitem <> "") then
mm_joinchar = "&"
else
mm_joinchar = ""
end if
end function
%>
<%
*** move to record: set the strings for the first, last, next, and previous links
dim mm_keepmove
dim mm_moveparam
dim mm_movefirst
dim mm_movelast
dim mm_movenext
dim mm_moveprev
dim mm_urlstr
dim mm_paramlist
dim mm_paramindex
dim mm_nextparam
mm_keepmove = mm_keepboth
mm_moveparam = "index"
if the page has a repeated region, remove offset from the maintained parameters
if (mm_size > 1) then
mm_moveparam = "offset"
if (mm_keepmove <> "") then
mm_paramlist = split(mm_keepmove, "&")
mm_keepmove = ""
for mm_paramindex = 0 to ubound(mm_paramlist)
mm_nextparam = left(mm_paramlist(mm_paramindex), instr(mm_paramlist(mm_paramindex),"=") – 1)
if (strcomp(mm_nextparam,mm_moveparam,1) <> 0) then
mm_keepmove = mm_keepmove & "&" & mm_paramlist(mm_paramindex)
end if
next
if (mm_keepmove <> "") then
mm_keepmove = right(mm_keepmove, len(mm_keepmove) – 1)
end if
end if
end if
set the strings for the move to links
if (mm_keepmove <> "") then
mm_keepmove = mm_keepmove & "&"
end if
mm_urlstr = request.servervariables("url") & "?" & mm_keepmove & mm_moveparam & "="
mm_movefirst = mm_urlstr & "0"
mm_movelast = mm_urlstr & "-1"
mm_movenext = mm_urlstr & cstr(mm_offset + mm_size)
if (mm_offset – mm_size < 0) then
mm_moveprev = mm_urlstr & "0"
else
mm_moveprev = mm_urlstr & cstr(mm_offset – mm_size)
end if
%>
<html>
<head>
<title>untitled document</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
</head>
<body>
<p></p>
<table border="1">
<tr>
<td>cpno</td>
<td>cbno</td>
<td>cdpt</td>
<td>cid</td>
<td>cbnm</td>
<td>ddate</td>
<td>cstate</td>
<td>guid</td>
</tr>
<% while ((repeat1__numrows <> 0) and (not recordset1.eof)) %>
<tr>
<td><%=(recordset1.fields.item("cpno").value)%></td>
<td><%=(recordset1.fields.item("cbno").value)%></td>
<td><%=(recordset1.fields.item("cdpt").value)%></td>
<td><%=(recordset1.fields.item("cid").value)%></td>
<td><%=(recordset1.fields.item("cbnm").value)%></td>
<td><%=(recordset1.fields.item("ddate").value)%></td>
<td><%=(recordset1.fields.item("cstate").value)%></td>
<td><%=(recordset1.fields.item("guid").value)%></td>
</tr>
<%
repeat1__index=repeat1__index+1
repeat1__numrows=repeat1__numrows-1
recordset1.movenext()
wend
%>
</table>
<p> </p>
<table border="0" width="50%" align="center">
<tr>
<td width="23%" align="center"> <% if mm_offset <> 0 then %>
<a href="<%=mm_movefirst%>"><img src="first.gif" border=0></a>
<% end if end mm_offset <> 0 %> </td>
<td width="31%" align="center"> <% if mm_offset <> 0 then %>
<a href="<%=mm_moveprev%>"><img src="previous.gif" border=0></a>
<% end if end mm_offset <> 0 %> </td>
<td width="23%" align="center"> <% if not mm_attotal then %>
<a href="<%=mm_movenext%>"><img src="next.gif" border=0></a>
<% end if end not mm_attotal %> </td>
<td width="23%" align="center"> <% if not mm_attotal then %>
<a href="<%=mm_movelast%>"><img src="last.gif" border=0></a>
<% end if end not mm_attotal %> </td>
</tr>
</table>
records <%=(recordset1_first)%> to <%=(recordset1_last)%> of <%=(recordset1_total)%>
</body>
</html>
<%
recordset1.close()
set recordset1 = nothing
%>
编译成dll用也可以:)
