Principle: first, create a server to process the paging page. You must determine whether the total number of returned data records or the data of a page are determined based on the action parameter.
Getcommentdata. ashx:
<% @ Webhandler Language = "C #" class = "getdatacount" %> using system; using system. web; public class getdatacount: ihttphandler {public void processrequest (httpcontext context) {context. response. contenttype = "text/plain"; var request = context. request; var response = context. response; string action = request ["action"]; jiang_db newdb = new jiang_db (); If (Action = "commentdatacount") // total number of returned data records {response. write (newdb. execsql_value ("select count (*) from [comment]");} else if (Action = "commentdatapage ") // return paging data {string page = request ["page"]; // page number string pagesize = request ["pagesize"]; // number of entries displayed on each page system. data. oledb. oledbdatareader reader = newdb. re_datareader ("select * from (select top" + pagesize + "* from (select top" + convert. toint32 (PAGE) * convert. toint32 (pagesize) + "* from [comment] Order by [ID]) order by [ID] DESC) order by ID"); response. write (jsonhelper. getjson (reader, "comment"); // serialize data in JSON format and return newdb. close () ;}} public bool isreusable {get {return false ;}}}
The Access database is used this time and does not support the row_number () over () function. Therefore, use the following complex SQL statement. for example, if we have 11 data records and 5 data records are displayed on each page, we need to divide the data into three pages. The SQL statement is as follows:
Select * from (select top 5 * from (select top 10 * from [comment] Order by [ID]) order by [ID] DESC) order by ID
The principle of this statement is, for example, to fetch 6-10 records, first retrieve the first 10 records and then retrieve the first 5 Records.
Here, if the server returns data by page, it cannot be a single piece of data, which is usually returned after being serialized in XML or JSON format, here we use a self-written JSON function to serialize the data in datareader and return a string. The returned results are in the following format:
Jsonhelper class:
Using system; using system. collections. generic; using system. text; using system. data; using system. data. sqlclient; using system. data. oledb; public sealed class jsonhelper {/** // obtain the JSON string sqldatareader // value // data table name // public static string getjson (sqldatareader drvalue, string strtablename) {system. text. stringbuilder sb = new system. text. stringbuilder (); // sb. appendline ("{"); // sb. appendline ("" + strtablename + ": {"); // sb. appendline ("records: ["); sb. appendline ("["); try {While (drvalue. read () {sb. append ("{"); For (INT I = 0; I <drvalue. fieldcount; I ++) {sb. appendformat ("\" {0} \ ": \" {1} \ ",", drvalue. getname (I), drvalue. getvalue (I);} sb. remove (sb. tostring (). lastindexof (','), 1); sb. appendline ("},");} sb. remove (sb. tostring (). lastindexof (','), 1);} catch (exception ex) {Throw new exception (ex. message);} finally {drvalue. close ();} sb. appendline ("]"); // sb. appendline ("}"); // sb. appendline ("};"); return sb. tostring ();} public static string getjson (oledbdatareader drvalue, string strtablename) {system. text. stringbuilder sb = new system. text. stringbuilder (); // sb. appendline ("{"); // sb. appendline ("" + strtablename + ": {"); // sb. appendline ("records: ["); sb. appendline ("["); try {While (drvalue. read () {sb. append ("{"); For (INT I = 0; I <drvalue. fieldcount; I ++) {sb. appendformat ("\" {0} \ ": \" {1} \ ",", drvalue. getname (I), drvalue. getvalue (I);} sb. remove (sb. tostring (). lastindexof (','), 1); sb. appendline ("},");} sb. remove (sb. tostring (). lastindexof (','), 1);} catch (exception ex) {Throw new exception (ex. message);} finally {drvalue. close ();} sb. appendline ("]"); // sb. appendline ("}"); // sb. appendline ("};"); return sb. tostring ();}}
After the server page is completed, write the following client page:
What the client needs to do is to send a request with parameters to the server, then accept the data returned by the server and parse it using the jqery parsejson () function, and then fill it on the page.
<! 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">
: