ASP uses jqgrid to implement read/write deletion code (JSON)

Source: Internet
Author: User
Tags jqgrid

Jqgrid is an excellent jquery-based DataGrid framework, which is certainly familiar to everyone. There are few ASP-based materials on the Internet. I provide one in JSON format:
, A json class for jqgrid: Code It seems to have been converted from some PHP on the official website forum. We save it as JSON. asp and paste the code below: Copy code The Code is as follows: <%
Response. charset = "UTF-8"
'---------------------------------------
'Jsonclass class
'Convert the execution result of the SELECT statement to JSON
'------------------------------------------
Class jsonclass
'Define class attributes. The default value is private.
Dim sqlstring 'is used to set select
Name of the JSON object returned by dim json'
Dim dbconnection 'Connection object connected to the database
'Public methods that can be called externally
Public Function getjson ()
Dim rs
Dim returnstr
Dim I
Dim onerecord
'Get data
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
'Generate a JSON string
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
'-------
'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
'After the last field of the record is removed ","
Onerecord = left (onerecord, limit Rev (onerecord, ",")-1)
Onerecord = onerecord & "]},"
'------------
Returnstr = returnstr & onerecord
Rs. movenext
Next
'Remove all record arrays ","
Returnstr = left (returnstr, Rev (returnstr, ",")-1)
Returnstr = returnstr & "]}"
End if
Rs. Close
Set rs = nothing
Getjson = returnstr
End Function
'Private method, used in the class
Private function check ()
End Function
'
End Class
%>

2. Create an ASP file that displays data, such as list. asp. The Code is as follows: Copy code The Code is as follows: <! -- # 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 ??
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, strsearchmetadata, strwhere
Strsearchon = request ("_ search ")
If (strsearchon = "true") then
Strfield = request ("searchfield ")
If (strfield = "ID" or strfield = "title" or strfield = "nickname") then
Strfielddata = request ("searchstring ")
Strsearchpipeline = request ("searchpipeline ")
'Construct where
Strwhere = "where" & strfield
Select case strsearchpipeline
Case "BW": 'beginwith
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
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
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
Strwhere = strwhere & "like '%" & strfielddata &"'"
Case "cn": 'contains'
Strwhere = strwhere & "like '%" & strfielddata & "% '"
End select
End if
End if
Server. scripttimeout = 9000
Dim
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
%>

Contains the search code. This basically achieves read. As for the editurl file in jqgrid, we call it edit. asp. The Code is as follows: Copy code The Code is as follows: <% option explicit %>
<! -- # Include file = "config. asp" -->
<%
Dim stroper, Strid, strnickname, strtitle, strpwd
Stroper = request ("response ")
Strid = Replace (Request ("ID "),"'","''")
Strtitle = Replace (Request ("title "),"'","''")
Strnickname = Replace (Request ("nickname "),"'","''")
Strpwd = Replace (Request ("PWD "),"'","''")
Select case stroper
Case "add": 'add record
Strsql = "insert into admin (title, nickname, PWD, lastlogintime) values ('" & strtitle & "', '" & strnickname &"', '"& strpwd &"', now ())"
Case "edit": 'edit record
Strsql = "Update admin set Title = '" & strtitle & "', nickname = '" & strnickname &"', pwd = '"& strpwd &" 'where id = "& Strid
Case "Del": 'delete record
Strsql = "delete from Admin where id =" & Strid
End select
'Response. Write strsql
Dim strsql, RS
Call opendb ()
Set rs = conn. Execute (strsql)
Call closedb ()
%>

This is the front-end index.html code. Copy code The Code is as follows: <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> asp_jqgrid_test </title>
<LINK rel = "stylesheet" type = "text/CSS" href = "jquery-ui-1.7.2.custom.css"/>
<LINK rel = "stylesheet" type = "text/CSS" href = "jqgrid.css"/>
<LINK rel = "stylesheet" type = "text/CSS" href = "ui.multiselect.css"/>
<SCRIPT type = "text/JavaScript" src = "JS/jquery. js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "JS/CN. js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "JS/jqgrid. js"> </SCRIPT>
</Head>
<Body>
<Table id = "DataGrid" class = "scroll"> </table>
<Div id = "pager" class = "scroll" style = "text-align: center;"> </div>
</Body>
</Html>
<SCRIPT type = "text/JavaScript">
Jquery ("# DataGrid"). jqgrid ({
URL: 'list. asp ',
Datatype: "JSON ",
Colnames: ['id', 'administrator account', 'administrator nickname ', 'Password', 'last logon time'],
Colmodel :[
{
Name: 'id ',
Index: 'id ',
Width: 50
},
{
Name: 'title ',
Index: 'title ',
Editable: True,
Editrules :{
Required: True
}
},
{
Name: 'nickname ',
Index: 'nickname ',
Editable: True,
Editrules :{
Required: True
}
},
{
Name: 'pwd ',
Index: 'pwd ',
Editable: True,
Edittype: 'Password ',
Hidden: True,
Editoptions :{
Size: 20
},
Editrules :{
Edithidden: True
}
},
{
Name: 'lastlogintime ',
Index: 'lastlogintime ',
Align: 'right ',
Editrules :{
Required: True
}
}], Caption: "Administrator list ",
Imgpath: '/images ',
Multiselect: True,
Rownum: 20,
Rowlist: [10, 20, 30],
Pager: jquery ('# pager '),
Sortname: 'id ',
Viewrecords: True,
Sortorder: "DESC ",
Height: 400,
Width: 600,
Editurl: "Edit. asp"
});
$ ('# DataGrid'). navgrid ('# pager ',{
Refresh: True,
Edit: True,
Add: True,
DEL: True,
Search: True,
Searchtext: "Search ",
Edittext: "modify", addtext: "add", deltext: "delete"
});
</SCRIPT>

Jqgrid, good stuff ~~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.