ASP 使用jqGrid实现读写删的代码(json)

时间:2010-03-07 17:26:00 

jqGrid是一个优秀的基于jQuery的DataGrid框架,想必大伙儿也不陌生,网上基于ASP的资料很少,我提供一个,数据格式是json的:

一个针对jqGrid的json类:这段代码似乎是由官网论坛的一些PHP中转化而来,我们存为json.asp,代码贴一下:


<%
response.Charset="utf-8"
'---------------------------------------
' JSONClass类
' 将Select语句的执行结果转换成JSON
'------------------------------------------
Class JSONClass
' 定义类属性,默认为Private
Dim SqlString ' 用于设置Select
Dim JSON ' 返回的JSON对象的名称
Dim DBConnection ' 连接到数据库的Connection对象
' 可以外部调用的公共方法
Public Function GetJSON ()
dim Rs
dim returnStr
dim i
dim oneRecord
' 获取数据
Set Rs= Server.CreateObject("ADODB.Recordset")
Rs.open SqlString,DBConnection,1,1
if page<>"" then
epage=cint(page)
if epage<1 then epage=1
if epage>rs.pagecount then epage=rs.pagecount
else
epage=1
end if
rs.pagesize = rows
rs.absolutepage = epage
' 生成JSON字符串
if Rs.eof=false and Rs.Bof=false then
returnStr="{ total: "& rs.pagecount &", page: "& page &", records: "& rs.recordcount &", rows:["
for j=0 to rs.pagesize-1
if rs.bof or rs.eof then exit for
' -------
'oneRecord = "{id:" & chr(34) &Rs.Fields(0).Value&chr(34)&",cell:["& chr(34) &Rs.Fields(0).Value&chr(34)&","
oneRecord = "{id:" & chr(34) &Rs.Fields(0).Value&chr(34)&",cell:["& chr(34) &Rs.Fields(0).Value&chr(34)&","
for i=1 to Rs.Fields.Count -1
'oneRecord=oneRecord & chr(34) &Rs.Fields(i).Name&chr(34)&":"
oneRecord=oneRecord & chr(34) &Rs.Fields(i).Value&chr(34) &","
Next
'去除记录最后一个字段后的","
oneRecord=left(oneRecord,InStrRev(oneRecord,",")-1)
oneRecord=oneRecord & "]},"
'------------
returnStr=returnStr & oneRecord
Rs.MoveNext
next
' 去除所有记录数组后的","
returnStr=left(returnStr,InStrRev(returnStr,",")-1)
returnStr=returnStr & "]}"
end if
Rs.close
set Rs=Nothing
GetJSON=returnStr
End Function
'私用方法,在类中使用
Private Function check()
End Function
'
End Class
%>

2、制作显示数据的asp文件,如:list.asp,代码如下

 


<!--#include file="conn.asp" -->
<!--#include file="json.asp" -->
<%
dim page,rows,sidx,sord
page = request.QueryString("page") 'page
rows = request.QueryString("rows") 'pagesize
sidx = request.QueryString("sidx") 'order by ??
sord = request.QueryString("sord")
if page="" then page = 1 end if
if rows = "" then rows = 10 end if
if sidx = "" then sidx = "id" end if
if sord = "" then sord ="asc" end if
Dim strSearchOn, strField, strFieldData, strSearchOper, strWhere
strSearchOn = Request("_search")
If (strSearchOn = "true") Then
strField = Request("searchField")
If (strField = "id" Or strField = "Title" Or strField = "NickName") Then
strFieldData = Request("searchString")
strSearchOper = Request("searchOper")
'construct where
strWhere = " Where " & strField
Select Case strSearchOper
Case "bw" : 'Begin With
strFieldData = strFieldData & "%"
strWhere = strWhere & " LIKE '" & strFieldData & "'"
Case "eq" : 'Equal
If(IsNumeric(strFieldData)) Then
strWhere = strWhere & " = " & strFieldData
Else
strWhere = strWhere & " = '" & strFieldData & "'"
End If
Case "ne": 'Not Equal
If(IsNumeric(strFieldData)) Then
strWhere = strWhere & " <> " & strFieldData
Else
strWhere = strWhere & " <> '"& strFieldData &"'"
End If
Case "lt": 'Less Than
If(IsNumeric(strFieldData)) Then
strWhere = strWhere & " <" & strFieldData
Else
strWhere = strWhere & " <'"& strFieldData &"'"
End If
Case "le": 'Less Or Equal
If(IsNumeric(strFieldData)) Then
strWhere = strWhere & " <= " & strFieldData
Else
strWhere = strWhere & " <= '"& strFieldData &"'"
End If
Case "gt": 'Greater Than
If(IsNumeric(strFieldData)) Then
strWhere = strWhere & " > " & strFieldData
Else
strWhere = strWhere & " > '"& strFieldData &"'"
End If
Case "ge": 'Greater Or Equal
If(IsNumeric(strFieldData)) Then
strWhere = strWhere & " >= " & strFieldData
Else
strWhere = strWhere & " >= '"& strFieldData &"'"
End If
Case "ew" : 'End With
strWhere = strWhere & " LIKE '%" & strFieldData & "'"
Case "cn" : 'Contains
strWhere = strWhere & " LIKE '%" & strFieldData & "%'"
End Select
End if
End If
server.ScriptTimeout=9000
dim a
set a=new JSONClass
a.Sqlstring="Select id,Title,NickName,Pwd,LastLoginTime From Admin"&strWhere&" "&"order by "& sidx & " " & sord
a.dbconnection=conn
response.Write(a.GetJSon())
conn.close()
set conn = nothing
%>

标签:jqGrid,asp,json
0
投稿

猜你喜欢

  • 自适应css布局——流动布局新时代[译]

    2009-08-13 12:28:00
  • 互联网产品的用户体验看着“很美”

    2009-07-07 12:04:00
  • Mootools 1.2教程(10)——Fx.Tween的使用

    2008-12-02 18:03:00
  • VBScript中清除数组元素Erase语句

    2008-06-27 13:05:00
  • 如何在ADO中使用SQL函数?

    2010-06-17 12:51:00
  • xWin之JS版

    2009-09-12 18:45:00
  • 网站设计配色方案教程

    2007-10-10 19:38:00
  • 使用Dreamweaver代码片断提高css开发效率

    2007-10-28 15:46:00
  • 实现div可编辑的常见方法

    2007-11-06 12:02:00
  • 巧妙规划使用Oracle数据空间

    2009-03-20 11:51:00
  • JS的IE和FF兼容性问题汇总

    2008-03-08 13:01:00
  • SQL Server命令行导数据的2种方式

    2010-07-26 14:48:00
  • asp如何验证信用卡是否可用?

    2010-06-10 18:39:00
  • Mootools常用方法扩展(四)

    2009-02-21 11:12:00
  • Dreamweaver量身打造Wordpress留言板(二)

    2009-12-12 17:35:00
  • SqlServer参数化查询之where in和like实现详解

    2012-05-22 18:10:50
  • asp伪继承初探_实例代码

    2011-04-19 10:32:00
  • MS SQL7.0的数据迁移到MySQL上的一种方法

    2008-11-01 16:59:00
  • SQL Server 2005中数据库镜像的四个问题

    2009-02-19 16:48:00
  • 使用SQL Server 2008管理非结构化数据

    2009-01-08 15:28:00
  • asp之家 网络编程 m.aspxhome.com