Classic ASP 使用jqGrid实现读写删(json)
因为有了比较成型的CMS,一直没有舍弃ASP,一直想用.net重写一下,是后话了。
jqGrid是一个优秀的基于jQuery的DataGrid框架,想必大伙儿也不陌生,网上基于ASP的资料很少,我提供一个,数据格式是json的:
1、一个针对jqGrid的json类:这段代码似乎是由官网论坛的一些PHP中转化而来,我们存为json.asp,代码贴一下:
代码1 <%
2 response.Charset="utf-8"
3 '---------------------------------------
4 ' JSONClass类
5 ' 将Select语句的执行结果转换成JSON
6 '------------------------------------------
7 Class JSONClass
8 ' 定义类属性,默认为Private
9 Dim SqlString ' 用于设置Select
10 Dim JSON ' 返回的JSON对象的名称
11 Dim DBConnection ' 连接到数据库的Connection对象
12
13 ' 可以外部调用的公共方法
14 Public Function GetJSON ()
15 dim Rs
16 dim returnStr
17 dim i
18 dim oneRecord
19
20 ' 获取数据
21 Set Rs= Server.CreateObject("ADODB.Recordset")
22 Rs.open SqlString,DBConnection,1,1
23
24 if page<>"" then
25 epage=cint(page)
26 if epage<1 then epage=1
27 if epage>rs.pagecount then epage=rs.pagecount
28 else
29 epage=1
30 end if
31
32 rs.pagesize = rows
33 rs.absolutepage = epage
34 ' 生成JSON字符串
35 if Rs.eof=false and Rs.Bof=false then
36 returnStr="{ total: "& rs.pagecount &", page: "& page &", records: "& rs.recordcount &", rows:["
37
38 for j=0 to rs.pagesize-1
39 if rs.bof or rs.eof then exit for
40 ' -------
41 'oneRecord = "{id:" & chr(34) &Rs.Fields(0).Value&chr(34)&",cell:["& chr(34) &Rs.Fields(0).Value&chr(34)&","
42 oneRecord = "{id:" & chr(34) &Rs.Fields(0).Value&chr(34)&",cell:["& chr(34) &Rs.Fields(0).Value&chr(34)&","
43 for i=1 to Rs.Fields.Count -1
44 'oneRecord=oneRecord & chr(34) &Rs.Fields(i).Name&chr(34)&":"
45 oneRecord=oneRecord & chr(34) &Rs.Fields(i).Value&chr(34) &","
46 Next
47 '去除记录最后一个字段后的","
48 oneRecord=left(oneRecord,InStrRev(oneRecord,",")-1)
49 oneRecord=oneRecord & "]},"
50 '------------
51 returnStr=returnStr & oneRecord
52 Rs.MoveNext
53 next
54
55 ' 去除所有记录数组后的","
56 returnStr=left(returnStr,InStrRev(returnStr,",")-1)
57 returnStr=returnStr & "]}"
58 end if
59 Rs.close
60 set Rs=Nothing
61 GetJSON=returnStr
62 End Function
63
64 '私用方法,在类中使用
65 Private Function check()
66
67 End Function
68 '
69 End Class
70 %>
71
2、制作显示数据的asp文件,如:list.asp,代码如下
代码
1 <!--#include file="conn.asp" -->
2 <!--#include file="json.asp" -->
3
4 <%
5
6 dim page,rows,sidx,sord
7 page = request.QueryString("page") 'page
8 rows = request.QueryString("rows") 'pagesize
9 sidx = request.QueryString("sidx") 'order by ??
10 sord = request.QueryString("sord")
11 if page="" then page = 1 end if
12 if rows = "" then rows = 10 end if
13 if sidx = "" then sidx = "id" end if
14 if sord = "" then sord ="asc" end if
15
16 Dim strSearchOn, strField, strFieldData, strSearchOper, strWhere
17
18 strSearchOn = Request("_search")
19 If (strSearchOn = "true") Then
20 strField = Request("searchField")
21 If (strField = "id" Or strField = "Title" Or strField = "NickName") Then
22 strFieldData = Request("searchString")
23 strSearchOper = Request("searchOper")
24 'construct where
25 strWhere = " Where " & strField
26
27 Select Case strSearchOper
28 Case "bw" : 'Begin With
29 strFieldData = strFieldData & "%"
30 strWhere = strWhere & " LIKE '" & strFieldData & "'"
31 Case "eq" : 'Equal
32 If(IsNumeric(strFieldData)) Then
33 strWhere = strWhere & " = " & strFieldData
34 Else
35 strWhere = strWhere & " = '" & strFieldData & "'"
36 End If
37 Case "ne": 'Not Equal
38 If(IsNumeric(strFieldData)) Then
39 strWhere = strWhere & " <> " & strFieldData
40 Else
41 strWhere = strWhere & " <> '"& strFieldData &"'"
42 End If
43 Case "lt": 'Less Than
44 If(IsNumeric(strFieldData)) Then
45 strWhere = strWhere & " <" & strFieldData
46 Else
47 strWhere = strWhere & " <'"& strFieldData &"'"
48 End If
49 Case "le": 'Less Or Equal
50 If(IsNumeric(strFieldData)) Then
51 strWhere = strWhere & " <= " & strFieldData
52 Else
53 strWhere = strWhere & " <= '"& strFieldData &"'"
54 End If
55 Case "gt": 'Greater Than
56 If(IsNumeric(strFieldData)) Then
57 strWhere = strWhere & " > " & strFieldData
58 Else
59 strWhere = strWhere & " > '"& strFieldData &"'"
60 End If
61 Case "ge": 'Greater Or Equal
62 If(IsNumeric(strFieldData)) Then
63 strWhere = strWhere & " >= " & strFieldData
64 Else
65 strWhere = strWhere & " >= '"& strFieldData &"'"
66 End If
67 Case "ew" : 'End With
68 strWhere = strWhere & " LIKE '%" & strFieldData & "'"
69 Case "cn" : 'Contains
70 strWhere = strWhere & " LIKE '%" & strFieldData & "%'"
71 End Select
72 End if
73 End If
74
75 server.ScriptTimeout=9000
76 dim a
77 set a=new JSONClass
78 a.Sqlstring="Select id,Title,NickName,Pwd,LastLoginTime From Admin"&strWhere&" "&"order by "& sidx & " " & sord
79 a.dbconnection=conn
80 response.Write(a.GetJSon())
81
82 conn.close()
83 set conn = nothing
84 %>
85
里面把搜索的代码涵盖了。这样基本实现了读,至于jqGrid中的editurl的文件,我们称其edit.asp,代码如下:
代码1 <%Option Explicit%>
2 <!--#include file="config.asp"-->
3 <%
4 Dim strOper, strID, strNickName, strTitle, strPwd
5
6 strOper = Request("oper")
7 strID = Replace(Request("Id"),"'","''")
8 strTitle = Replace(Request("Title"),"'","''")
9 strNickName = Replace(Request("NickName"),"'","''")
10 strPwd = Replace(Request("Pwd"),"'","''")
11 Select Case strOper
12 Case "add": 'Add Record
13 strSQL = "Insert Into Admin (Title, NickName, Pwd,LastLoginTime) Values('"&strTitle&"', '"&strNickName&"', '"&strPwd&"',Now()) "
14 Case "edit": 'Edit Record
15 strSQL = "Update Admin Set Title = '"&strTitle&"', NickName = '"&strNickName&"', Pwd = '"&strPwd&"' Where id = "&strID
16 Case "del": 'Delete Record
17 strSQL = "Delete From Admin Where id = "&strID
18 End Select
19 'response.Write strSQL
20 Dim strSQL,rs
21 Call OpenDB()
22 Set rs = Conn.Execute(strSQL)
23 Call CloseDB()
24 %>
25
这是前台index.html代码
代码
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>ASP_jqGrid_Test</title>
6 <link rel="stylesheet" type="text/css" href="jquery-ui-1.7.2.custom.css"/>
7 <link rel="stylesheet" type="text/css" href="jqgrid.css"/>
8 <link rel="stylesheet" type="text/css" href="ui.multiselect.css"/>
9 <script type="text/javascript" src="js/jquery.js"></script>
10 <script type="text/javascript" src="js/cn.js"></script>
11 <script type="text/javascript" src="js/jqGrid.js"></script>
12
13 </head>
14
15 <body>
16 <table id="DataGrid" class="scroll"></table>
17 <div id="pager" class="scroll" style="text-align:center;"></div>
18 </body>
19 </html>
20
21 <script type="text/javascript">
22 jQuery("#DataGrid").jqGrid({
23 url:'list.asp',
24 datatype: "json",
25 colNames:['ID','管理员账号','管理员昵称','密码','上次登录时间'],
26 colModel :[
27 {
28 name:'Id',
29 index:'Id',
30 width:50
31 },
32 {
33 name:'Title',
34 index:'Title',
35 editable:true,
36 editrules:{
37 required:true
38 }
39 },
40 {
41 name:'NickName',
42 index:'NickName',
43 editable:true,
44 editrules:{
45 required:true
46 }
47 },
48 {
49 name:'Pwd',
50 index:'Pwd',
51 editable:true,
52 edittype:'password',
53 hidden:true,
54 editoptions:{
55 size:20
56 },
57 editrules:{
58 edithidden:true
59 }
60 },
61
62 {
63 name:'LastLoginTime',
64 index:'LastLoginTime',
65 align:'right',
66 editrules:{
67 required:true
68 }
69 } ], caption:"管理员列表",
70 imgpath:'/images',
71 multiselect: true,
72 rowNum:20,
73 rowList:[10,20,30],
74 pager: jQuery('#pager'),
75 sortname: 'Id',
76 viewrecords: true,
77 sortorder: "desc",
78 height:400,
79 width:600,
80 editurl:"edit.asp"
81 });
82 $('#DataGrid').navGrid('#pager',{
83 refresh: true,
84 edit: true,
85 add: true,
86 del: true,
87 search: true,
88 searchtext:"搜",
89 edittext:"改",addtext:"添",deltext:"删"
90
91 });
92
93
94
95 </script>
96
附源代码:
不带数据库... 下载
jqGrid,好东西~~