Learning easyui-datagrid step by step based on the asp.net + easyui framework to implement paging and search (2)

Source: Internet
Author: User
Tags tojson

Directory:

Learning easyui-datagrid step by step based on asp.net + easyui framework (I)

Learning easyui-datagrid step by step based on the asp.net + easyui framework to implement paging and search (2)

Based on the asp.net + easyui framework, learn easyui-datagrid step by step to implement addition, editing, and deletion (3)

Learning easyui-datagrid step by step based on asp.net + easyui framework. Summary (4)


In the previous blog, I only completed part of the interface and continued the content of the previous blog. In this blog, We need to display the records in the database on the interface and display the data on pages.

I once wrote a paging blog. Paging is very simple. The essential difference lies in the way in which information is read from the database during paging: false paging: one-time Data Reading; real paging: Multiple Data Reading. The datagrid uses a real page and queries records from the database.

The following figure shows the implementation interface:

VcHL0rvG8KGjPC9wPgo8cD48YnI + cda-vcd4kpha + 5E + PC9wPgo8cHJlIGNsYXNzPQ = "brush: java;"> using System; using System. collections. generic; using System. linq; using System. web; using System. data; using System. text; namespace GoodCommunitySystem. admin. userManager {///

/// Summary of SetAdmin ///Public class SetAdmin: IHttpHandler {BLL. adminInfoBLL admininfobll = new BLL. adminInfoBLL (); Entity. adminInfoEntity enadmininfo = new Entity. adminInfoEntity (); public void ProcessRequest (HttpContext context) {// call the Query method Query (context);} public bool IsReusable {get {return false ;}}/// /// Query records ////// Public void Query (HttpContext context) {context. response. contentType = "text/plain "; // ================================================ ==========================================/// obtain the query conditions: [user ID, start time, end time, keyword] string AdminName, startTime, endTime, QuanXian; AdminName = startTime = endTime = QuanXian = ""; // obtain the value if (null! = Context. request. queryString ["AdminName"]) {// obtain the value AdminName = context from the foreground. request. queryString ["AdminName"]. toString (). trim ();} if (null! = Context. Request. QueryString ["StartTime"]) {startTime = context. Request. QueryString ["StartTime"]. ToString (). Trim ();} if (null! = Context. Request. QueryString ["EndTime"]) {endTime = context. Request. QueryString ["EndTime"]. ToString (). Trim ();} if (null! = Context. request. queryString ["QuanXian"]) {QuanXian = context. request. queryString ["QuanXian"]. toString (). trim ();} // ================================================ ============================================/// retrieve paging and sorting information: page size, page number, sorting method, sorting field int pageRows, page; pageRows = 10; page = 1; string order, sort, oderby; order = sort = oderby = ""; if (null! = Context. request. queryString ["rows"]) {pageRows = int. parse (context. request. queryString ["rows"]. toString (). trim ();} if (null! = Context. request. queryString ["page"]) {page = int. parse (context. request. queryString ["page"]. toString (). trim ();} if (null! = Context. Request. QueryString ["sort"]) {order = context. Request. QueryString ["sort"]. ToString (). Trim ();} if (null! = Context. request. queryString ["order"]) {sort = context. request. queryString ["order"]. toString (). trim ();} // ================================================ ==========================================/// combined query statement: condition + sort StringBuilder strWhere = new StringBuilder (); if (AdminName! = "") {StrWhere. AppendFormat ("WorkerRealName like '% {0} %' and", AdminName);} if (QuanXian! = "") {StrWhere. AppendFormat ("AdminRightName like '% {0} %' and", QuanXian);} if (startTime! = "") {StrWhere. AppendFormat ("ActiveDate> = '{0}' and", startTime);} if (endTime! = "") {StrWhere. appendFormat ("ActiveDate <= '{0}' and", endTime) ;}// Delete unnecessary and int startindex = strWhere. toString (). lastIndexOf ("and"); // obtain the last and position if (startindex> = 0) {strWhere. remove (startindex, 3); // Delete unnecessary and keywords} if (sort! = "" & Order! = "") {// StrWhere. appendFormat ("order by {0} {1}", sort, order); // Add sort oderby = order + "" + sort;} // DataSet ds = Bnotice. getList (strWhere. toString (); // call getlist without pagination // call the GetList method of pagination DataSet ds = admininfobll. getListByPage (strWhere. toString (), oderby, (page-1) * pageRows + 1, page * pageRows); int count = admininfobll. getRecordCount (strWhere. toString (); // obtain the number of entries string strJson = ToJson. dataset2Json (ds, count); // converts DataSet data to Json data context. response. write (strJson); // return it to the context on the foreground page. response. end ();}}


Two paging methods are implemented by calling layer D:

////// Obtain the total number of records ///Public int GetRecordCount (string strWhere) {StringBuilder strSql = new StringBuilder (); strSql. Append ("select count (1) FROM V_admin_MgPersonFiles"); if (strWhere. Trim ()! = "") {StrSql. append ("where" + strWhere);} object obj = DbHelperSQL. getSingle (strSql. toString (); if (obj = null) {return 0;} else {return Convert. toInt32 (obj );}}////// Retrieve the data list by PAGE ///Public DataSet GetListByPage (string strWhere, string orderby, int startIndex, int endIndex) {StringBuilder strSql = new StringBuilder (); strSql. append ("SELECT * FROM ("); strSql. append ("SELECT ROW_NUMBER () OVER ("); if (! String. isNullOrEmpty (orderby. trim () {strSql. append ("order by T. "+ orderby);} else {strSql. append ("order by T. adminID desc ");} strSql. append (") AS Row, T. * from V_admin_MgPersonFiles T "); if (! String. isNullOrEmpty (strWhere. trim () {strSql. append ("WHERE" + strWhere);} strSql. append (") TT"); strSql. appendFormat ("where tt. row between {0} and {1} ", startIndex, endIndex); return DbHelperSQL. query (strSql. toString ());}


Another difficulty is to convert the dataset of dataset to json format. Below I encapsulate a class: ToJson

Public class ToJson {# convert region DataSet to Json format ////// Convert DataSet to Json format //////DataSet///
 Public static string Dataset2Json (DataSet ds, int total =-1) {StringBuilder json = new StringBuilder (); foreach (DataTable dt in ds. tables) {// {"total": 5, "rows": [json. append ("{\" total \ ":"); if (total =-1) {json. append (dt. rows. count);} else {json. append (total);} json. append (", \" rows \ ": ["); json. append (DataTable2Json (dt); json. append ("]}");} return json. toString () ;}# endregion # region dataTable is converted to Json format ////// DataTable converted to Json format /////////
 Public static string DataTable2Json (DataTable dt) {StringBuilder jsonBuilder = new StringBuilder (); for (int I = 0; I <dt. rows. count; I ++) {jsonBuilder. append ("{"); for (int j = 0; j <dt. columns. count; j ++) {jsonBuilder. append ("\" "); jsonBuilder. append (dt. columns [j]. columnName); jsonBuilder. append ("\": \ ""); jsonBuilder. append (dt. rows [I] [j]. toString (); jsonBuilder. append ("\", ");} if (dt. columns. count> 0) {jsonBuilder. remove (jsonBuilder. length-1, 1);} jsonBuilder. append ("},");} if (dt. rows. count> 0) {jsonBuilder. remove (jsonBuilder. length-1, 1);} return jsonBuilder. toString () ;}# convert endregion able to Json format}


This blog shows you how to implement the paging and search functions of the datagrid. Like advertising, It's so easy...


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.