dreamweaverMX已经正式发布了,Deamweaver4 + Deamweaver ULTRADEV 4 的组合使他成为当然的制做网页的首选工具,(好象做广告:) )
好了,进入正题,
我在以前做网页的分页时候都是用自己写的服务端脚本(我从不用ADO的分页),用了MX后发现在这里面用分页太方便了,不过代码也有点太长了,大家看下面的代码就可以知道。用过之后我发现里面recordset 的cursortype设为0分页竟然可以正常工作!这令我吃惊不少,分析了代码之后才发现MX 是用了一种挺笨的方法实现的,效率很低,所以大家还是用1吧:)
分析如下:
1@LANGUAGE="VBSCRIPT" CODEPAGE="936"
1
2Dim Recordset1
3Dim Recordset1_numRows
4
5Set Recordset1 = Server.CreateObject("ADODB.Recordset")
6Recordset1.ActiveConnection = MM_ncarcnn_STRING
7Recordset1.Source = "SELECT * FROM dbo.ncarinfo"
8
9Recordset1.CursorType = 0
10'这里用0也可以正常运行,但是经过分析代码可以看出,用0的效率很低,建议用1
11
12
13Recordset1.CursorLocation = 2
14Recordset1.LockType = 1
15Recordset1.Open()
16
17Recordset1_numRows = 0
1
2'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
3'以下为分页代码
4Dim Repeat1__numRows
5Dim Repeat1__index
6
7Repeat1__numRows = 10
8Repeat1__index = 0
9Recordset1_numRows = Recordset1_numRows + Repeat1__numRows
1
2' *** Recordset状态, 定义状态变量
3
4Dim Recordset1_total
5Dim Recordset1_first
6Dim Recordset1_last
7
8' set the record count
9Recordset1_total = Recordset1.RecordCount
10
11' set the number of rows displayed on this page
12If (Recordset1_numRows < 0) Then
13Recordset1_numRows = Recordset1_total
14Elseif (Recordset1_numRows = 0) Then
15Recordset1_numRows = 1
16End If
17
18' set the first and last displayed record
19Recordset1_first = 1
20Recordset1_last = Recordset1_first + Recordset1_numRows - 1
21
22' if we have the correct record count, check the other stats 处理正确的rs
23If (Recordset1_total <> -1) Then
24If (Recordset1_first > Recordset1_total) Then
25Recordset1_first = Recordset1_total
26End If
27If (Recordset1_last > Recordset1_total) Then
28Recordset1_last = Recordset1_total
29End If
30If (Recordset1_numRows > Recordset1_total) Then
31Recordset1_numRows = Recordset1_total
32End If
33End If
1
2' *** Recordset Stats: if we don't know the record count, manually count them处理错误的RS
3
4If (Recordset1_total = -1) Then
5
6' count the total records by iterating through the recordset
7Recordset1_total=0
8While (Not Recordset1.EOF)
9Recordset1_total = Recordset1_total + 1
10Recordset1.MoveNext
11Wend
12
13' reset the cursor to the beginning
14If (Recordset1.CursorType > 0) Then
15Recordset1.MoveFirst
16Else
17Recordset1.Requery
18End If
19
20' set the number of rows displayed on this page
21If (Recordset1_numRows < 0 Or Recordset1_numRows > Recordset1_total) Then
22Recordset1_numRows = Recordset1_total
23End If
24
25' set the first and last displayed record
26Recordset1_first = 1
27Recordset1_last = Recordset1_first + Recordset1_numRows - 1
28
29If (Recordset1_first > Recordset1_total) Then
30Recordset1_first = Recordset1_total
31End If
32If (Recordset1_last > Recordset1_total) Then
33Recordset1_last = Recordset1_total
34End If
35
36End If
1
2Dim MM_paramName
1
2' *** Move To Record and Go To Record: declare variables
3
4Dim MM_rs
5Dim MM_rsCount
6Dim MM_size
7Dim MM_uniqueCol
8Dim MM_offset
9Dim MM_atTotal
10Dim MM_paramIsDefined
11
12Dim MM_param
13Dim MM_index
14
15Set MM_rs = Recordset1
16MM_rsCount = Recordset1_total
17MM_size = Recordset1_numRows
18MM_uniqueCol = ""
19MM_paramName = ""
20MM_offset = 0
21MM_atTotal = false
22MM_paramIsDefined = false
23If (MM_paramName <> "") Then
24MM_paramIsDefined = (Request.QueryString(MM_paramName) <> "")
25End If
1
2' *** Move To Record: handle 'index' or 'offset' parameter
3
4if (Not MM_paramIsDefined And MM_rsCount <> 0) then
5
6' use index parameter if defined, otherwise use offset parameter
7MM_param = Request.QueryString("index")
8If (MM_param = "") Then
9MM_param = Request.QueryString("offset")
10End If
11If (MM_param <> "") Then
12MM_offset = Int(MM_param)
13End If
14
15' if we have a record count, check if we are past the end of the recordset
16If (MM_rsCount <> -1) Then
17If (MM_offset >= MM_rsCount Or MM_offset = -1) Then ' past end or move last
18If ((MM_rsCount Mod MM_size) > 0) Then ' last page not a full repeat region
19MM_offset = MM_rsCount - (MM_rsCount Mod MM_size)
20Else
21MM_offset = MM_rsCount - MM_size
22End If
23End If
24End If
25
26' move the cursor to the selected record
27MM_index = 0
28While ((Not MM_rs.EOF) And (MM_index < MM_offset Or MM_offset = -1))
29MM_rs.MoveNext
30MM_index = MM_index + 1
31Wend
32If (MM_rs.EOF) Then
33MM_offset = MM_index ' set MM_offset to the last possible record
34End If
35
36End If
1
2' *** Move To Record: if we dont know the record count, check the display range
3
4If (MM_rsCount = -1) Then
5
6' walk to the end of the display range for this page
7MM_index = MM_offset
8While (Not MM_rs.EOF And (MM_size < 0 Or MM_index < MM_offset + MM_size))
9MM_rs.MoveNext
10MM_index = MM_index + 1
11Wend
12
13' if we walked off the end of the recordset, set MM_rsCount and MM_size
14If (MM_rs.EOF) Then
15MM_rsCount = MM_index
16If (MM_size < 0 Or MM_size > MM_rsCount) Then
17MM_size = MM_rsCount
18End If
19End If
20
21' if we walked off the end, set the offset based on page size
22If (MM_rs.EOF And Not MM_paramIsDefined) Then
23If (MM_offset > MM_rsCount - MM_size Or MM_offset = -1) Then
24If ((MM_rsCount Mod MM_size) > 0) Then
25MM_offset = MM_rsCount - (MM_rsCount Mod MM_size)
26Else
27MM_offset = MM_rsCount - MM_size
28End If
29End If
30End If
31
32' reset the cursor to the beginning
33If (MM_rs.CursorType > 0) Then
34MM_rs.MoveFirst
35Else
36MM_rs.Requery
37End If
38
39' move the cursor to the selected record
40MM_index = 0
41While (Not MM_rs.EOF And MM_index < MM_offset)
42MM_rs.MoveNext
43MM_index = MM_index + 1
44Wend
45End If
1
2' *** Move To Record: update recordset stats
3
4' set the first and last displayed record
5Recordset1_first = MM_offset + 1
6Recordset1_last = MM_offset + MM_size
7
8If (MM_rsCount <> -1) Then
9If (Recordset1_first > MM_rsCount) Then
10Recordset1_first = MM_rsCount
11End If
12If (Recordset1_last > MM_rsCount) Then
13Recordset1_last = MM_rsCount
14End If
15End If
16
17' set the boolean used by hide region to check if we are on the last record
18MM_atTotal = (MM_rsCount <> -1 And MM_offset + MM_size >= MM_rsCount)
1
2' *** Go To Record and Move To Record: create strings for maintaining URL and Form parameters
3
4Dim MM_keepNone
5Dim MM_keepURL
6Dim MM_keepForm
7Dim MM_keepBoth
8
9Dim MM_removeList
10Dim MM_item
11Dim MM_nextItem
12
13' create the list of parameters which should not be maintained
14MM_removeList = "&index="
15If (MM_paramName <> "") Then
16MM_removeList = MM_removeList & "&" & MM_paramName & "="
17End If
18
19MM_keepURL=""
20MM_keepForm=""
21MM_keepBoth=""
22MM_keepNone=""
23
24' add the URL parameters to the MM_keepURL string
25For Each MM_item In Request.QueryString
26MM_nextItem = "&" & MM_item & "="
27If (InStr(1,MM_removeList,MM_nextItem,1) = 0) Then
28MM_keepURL = MM_keepURL & MM_nextItem & Server.URLencode(Request.QueryString(MM_item))
29End If
30Next
31
32' add the Form variables to the MM_keepForm string
33For Each MM_item In Request.Form
34MM_nextItem = "&" & MM_item & "="
35If (InStr(1,MM_removeList,MM_nextItem,1) = 0) Then
36MM_keepForm = MM_keepForm & MM_nextItem & Server.URLencode(Request.Form(MM_item))
37End If
38Next
39
40' create the Form + URL string and remove the intial '&' from each of the strings
41MM_keepBoth = MM_keepURL & MM_keepForm
42If (MM_keepBoth <> "") Then
43MM_keepBoth = Right(MM_keepBoth, Len(MM_keepBoth) - 1)
44End If
45If (MM_keepURL <> "") Then
46MM_keepURL = Right(MM_keepURL, Len(MM_keepURL) - 1)
47End If
48If (MM_keepForm <> "") Then
49MM_keepForm = Right(MM_keepForm, Len(MM_keepForm) - 1)
50End If
51
52' a utility function used for adding additional parameters to these strings
53Function MM_joinChar(firstItem)
54If (firstItem <> "") Then
55MM_joinChar = "&"
56Else
57MM_joinChar = ""
58End If
59End Function
1
2' *** Move To Record: set the strings for the first, last, next, and previous links
3
4Dim MM_keepMove
5Dim MM_moveParam
6Dim MM_moveFirst
7Dim MM_moveLast
8Dim MM_moveNext
9Dim MM_movePrev
10
11Dim MM_urlStr
12Dim MM_paramList
13Dim MM_paramIndex
14Dim MM_nextParam
15
16MM_keepMove = MM_keepBoth
17MM_moveParam = "index"
18
19' if the page has a repeated region, remove 'offset' from the maintained parameters
20If (MM_size > 1) Then
21MM_moveParam = "offset"
22If (MM_keepMove <> "") Then
23MM_paramList = Split(MM_keepMove, "&")
24MM_keepMove = ""
25For MM_paramIndex = 0 To UBound(MM_paramList)
26MM_nextParam = Left(MM_paramList(MM_paramIndex), InStr(MM_paramList(MM_paramIndex),"=") - 1)
27If (StrComp(MM_nextParam,MM_moveParam,1) <> 0) Then
28MM_keepMove = MM_keepMove & "&" & MM_paramList(MM_paramIndex)
29End If
30Next
31If (MM_keepMove <> "") Then
32MM_keepMove = Right(MM_keepMove, Len(MM_keepMove) - 1)
33End If
34End If
35End If
36
37' set the strings for the move to links
38If (MM_keepMove <> "") Then
39MM_keepMove = MM_keepMove & "&"
40End If
41
42MM_urlStr = Request.ServerVariables("URL") & "?" & MM_keepMove & MM_moveParam & "="
43
44MM_moveFirst = MM_urlStr & "0"
45MM_moveLast = MM_urlStr & "-1"
46MM_moveNext = MM_urlStr & CStr(MM_offset + MM_size)
47If (MM_offset - MM_size < 0) Then
48MM_movePrev = MM_urlStr & "0"
49Else
50MM_movePrev = MM_urlStr & CStr(MM_offset - MM_size)
51End If
1<html>
2<head>
3<title>Untitled Document</title>
4<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
5</head>
6<body>
7<p></p>
8<table border="1">
9<tr>
10<td>cpno</td>
11<td>cbno</td>
12<td>cdpt</td>
13<td>cid</td>
14<td>cbnm</td>
15<td>ddate</td>
16<td>cstate</td>
17<td>guid</td>
18</tr>
While ((Repeat1__numRows <> 0) AND (NOT Recordset1.EOF))
1<tr>
2<td>```
3=(Recordset1.Fields.Item("cpno").Value)
4```</td>
5<td>```
6=(Recordset1.Fields.Item("cbno").Value)
7```</td>
8<td>```
9=(Recordset1.Fields.Item("cdpt").Value)
10```</td>
11<td>```
12=(Recordset1.Fields.Item("cid").Value)
13```</td>
14<td>```
15=(Recordset1.Fields.Item("cbnm").Value)
16```</td>
17<td>```
18=(Recordset1.Fields.Item("ddate").Value)
19```</td>
20<td>```
21=(Recordset1.Fields.Item("cstate").Value)
22```</td>
23<td>```
24=(Recordset1.Fields.Item("guid").Value)
25```</td>
26</tr>
Repeat1__index=Repeat1__index+1
Repeat1__numRows=Repeat1__numRows-1
Recordset1.MoveNext()
Wend
1
2</table>
3<p> </p>
4<table align="center" border="0" width="50%">
5<tr>
6<td align="center" width="23%"> ```
7 If MM_offset &lt;&gt; 0 Then
<a href="``` =MM_moveFirst
End If ' end MM_offset <> 0
1<td align="center" width="31%"> ```
2 If MM_offset &lt;&gt; 0 Then
<a href="``` =MM_movePrev
End If ' end MM_offset <> 0
1<td align="center" width="23%"> ```
2 If Not MM_atTotal Then
<a href="``` =MM_moveNext
End If ' end Not MM_atTotal
1<td align="center" width="23%"> ```
2 If Not MM_atTotal Then
<a href="``` =MM_moveLast
End If ' end Not MM_atTotal
1</tr>
2</table>
3
4Records ```
5=(Recordset1_first)
6``` to ```
7=(Recordset1_last)
8``` of ```
9=(Recordset1_total)