Next http://www.cnblogs.com/bestfc/archive/2010/06/07/1753216.html
This article provides classes for encoding data
1. Create the ajaxdata class, inherit the ihttphandler interface, and implement its method public void processrequest (httpcontext context) and parameter public bool isreusable
2. Use the sqlhelper class to interact with the database. (entity framework can be used in MVC to provide data. Of course, in MVC, it is best not to use server controls. You can directly output strings)
3. Create two methods in the ajaxdata class.
Public String jsonstring (httpcontext context) is used to process the received basic data. Finally, the JSON string of the data is returned using the following method:
ItsCodeAs follows:
Public String Jsonstring (httpcontext context ){ // Params from request. querystring String Tablename = context. Request. querystring [" Tablename "]. Tostring (); String Xmlpath = system. appdomain. currentdomain. basedirectory + tablename +" . Xml "; Int _ Currentpage = Int . Parse (context. Request. querystring [" Page "]. Tostring ()); // Get the context. requested page Int _ Pagestep = Int . Parse (context. Request. querystring [" Rows "]. Tostring ()); // Get how many rows we want to have into the grid String Sidx = context. Request. querystring [" Sidx "]. Tostring (); // Get index row-I. e. User click to sort String Sord = context. Request. querystring [" Sord "]. Tostring (); // Get the direction // Params from XML Xmldocument xmldoc = New Xmldocument (); xmldoc. Load (xmlpath); xmlnode xnroot = xmldoc. selectsinglenode ("Root "); String Fields = xnroot. attributes [" Fields "]. Value; String Fixcondition = String . Empty; // Fixed the limit? Parts t If (Xnroot. attributes [" Fixcondition "]! = Null ) Fixcondition = xnroot. attributes [" Fixcondition "]. Value; datatable dt = sqlhelper. getdatabypager (fields, tablename ,"1 = 1 "+ Fixcondition + searchcase, sidx +" "+ Sord, (_ currentpage-1), _ pagestep ); Int _ Recordcount = sqlhelper. getrecordcount (tablename ," 1 = 1 "+ Fixcondition + searchcase ); Int _ Totalpages; If (_ Recordcount % _ pagestep = 0) {_ totalpages = _ recordcount/_ pagestep ;} Else {_ Totalpages = _ recordcount/_ pagestep + 1 ;} If (_ Recordcount = 0 ){ Return String . Empty ;} Else { Return Datatabletojson (DT, _ currentpage, _ totalpages, _ recordcount, xmldoc );}}
The sql2005 stored procedure provided by sqlhelper. CS is used.
Public String datatabletojson (datatable DT, int page, int total, int records, xmldocument xmldoc) is used to convert datatable to jqgrid Format String. The Code is as follows:
Public String Datatabletojson (datatable DT,Int Page, Int Total, Int Records, xmldocument xmldoc) {xmlnodelist xnlist = xmldoc. selectnodes (" Root // Columns "); String Idkey = String . Empty; Foreach (Xmlnode XN In Xnlist ){ If (Xn. attributes [" Isidentity "]! = Null ) {Idkey = xn. attributes [" Name "]. Value ;}} stringbuilder sb = New Stringbuilder (); sb. append (" { "); Sb. append (" \ "Page \": "+ Page +" , "); Sb. append (" \ "Total \": "+ Total +" , "); Sb. append (" \ "Records \": "+ Records +" , ");// ----------- Rows build SB. append (" \ "Rows \":[ "); Foreach (Datarow Dr In DT. Rows) {sb. append (" {\ "ID \":\" "+ Dr [idkey] +" \", "); Sb. append (" \ "Cell \":[ "); // ----------- Columns build Foreach (Xmlnode XN In Xnlist ){If (Xn. attributes [" Name "]. Value! = Idkey ){ String Values = Dr [XN. attributes [" Name "]. Value]. tostring (); If (Values. indexof (" \ N ")> = 0) // Solve the "unterminated String constant" problem when the JSON string contains the carriage return. {Values = values. Replace (" \ N "," \ N ");}Else If (Values. indexof ('" ')> = 0) // solve "A json string fault occurs. Use Chinese" to resolve {values = values. Replace ('" ',' ° '); } If (Xn. attributes [" Sorttype "]! = Null ){ Switch (Xn. attributes [" Sorttype "]. Value ){Case " Data ": If (Dr [XN. attributes [" Name "]. Value]! = Dbnull. Value) {sb. append (" \" "+ Datetime. parse (values) +" \", ");} Else {Sb. append (" \"\", ");}Break ; Case " Float ": If (Dr [XN. attributes [" Name "]. Value]! = Dbnull. Value) {sb. append (" \" "+ Decimal. parse (values). tostring (" 0.00 ") +" \", ");} Else {Sb. append (" \"\", ");} Break ; Default : SB. append (" \" "+ Values +" \", "); Break ;}} Else {Sb. append (" \" "+ Values +" \", ") ;}} Sb. Remove (sb. Length-1, 1 ); // ----------- End columns build SB. append (" ] "); Sb. append (" }, ");} Sb. Remove (sb. tostring (). Length-1, 1); sb. append (" ] "); // ---------- End rows build SB. append (" } "); Return SB. tostring ();}
You can see from the above two methods that a querystring is defined in the passed value, tablename is used to obtain the corresponding configuration file, or the reflection method can be used to provide data, however, it is not flexible.
4. In the processrequest method, encode the corresponding data for the front-end action feedback. The following are some codes:
If(Context. Request. querystring ["Action"]! =Null){StringActions = context. Request. querystring ["Action"]. Tostring ();Switch(Actions ){Case"View": Context. response. Write (jsonstring (context ));
In the passed value, a querystring is customized,ActionIt is used to identify the actions passed by the foreground and to query, edit, modify, delete, and other functions.
Here, only the feedback data is used for front-end display.
In this way, ajaxdata is complete.
Next, you can create a foreground project to obtain data from the above method, and declare httphandlers in Web. config:
<Httphandlers> <add Path ="Data. ashx"Verb ="*"Type ="Aspjqgrid. ajaxdata, aspjqgrid"/> </Httphandlers>
Located under the system. Web node, the page request is located in the ajaxdata class of aspjqgrid.
In addition, the [Assembly: tagprefix ("aspjqgrid", "allenjqgrid")] is added to the control code to declare the global
<Pages> <controls> <add tagprefix ="Allenjqgrid"Assembly ="Aspjqgrid"Namespace="Aspjqgrid"/> </Controls> </pages>The first step of the hello World display is completed, and the next step starts the encoding query function.
In this way, add <allenjqgrid: jqgrid id = "myjqgrid" runat = "server" tablename = "diamond"/>