Reference: http://www.jb51.net/article/35110.htm Improvements: 1. ashx returns json data, reducing the amount of transmitted data, and html page style control It is also more flexible; 2. Rewrite the jQuery code of the html page; 3. Simplify 3 ashx files into 1. 1. Create test data for the table:
public class Handler : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; StringBuilder tb = new StringBuilder(); DataBase db = new DataBase(); int pageSize = 10; int pageIndex = 0; string type = context.Request.Params["type"]; switch (type) { case "first": DataTable dt1 = db.GetDataSet("select top 10 * from test", null).Tables[0]; tb.Append(Common.DataTableToJSON(dt1, true)); //DataTable转为JSON break; case "next": pageIndex = Convert.ToInt32(context.Request.Params["index"]); DataTable dt2 = db.GetDataSet("select top " pageSize.ToString() " * from test where id> (select max(id) from (select top " (pageSize * pageIndex).ToString() " id from test) t)", null).Tables[0]; tb.Append(Common.DataTableToJSON(dt2, true)); break; case "pre": pageIndex = Convert.ToInt32(context.Request.Params["index"]); DataTable dt3 = db.GetDataSet("select top " pageSize.ToString() " * from test where id> (select max(id) from (select top " (pageSize * pageIndex).ToString() " id from test) t)", null).Tables[0]; tb.Append(JSONHelper.DataTableToJSON(dt)); break; } context.Response.Write(tb.ToString()); } public bool IsReusable { get { return false; } } }
public class Handler : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; DataBase db = new DataBase(); int pageSize = 10; int pageIndex = 0; int.TryParse(context.Request.Params["index"], out pageIndex); string type = context.Request.Params["type"]; string sql = string.Format("select * from ( select row_number() over (order by id) as rowNum,* from test) as t " " where rowNum>{0} and rowNum<={1}", pageIndex * pageSize, (pageIndex 1) * pageSize); DataTable dt = db.GetDataSet(sql, null).Tables[0]; context.Response.Write(JSONHelper.DataTableToJSON(dt)); } public bool IsReusable { get { return false; } } }
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn