Qgrid Series Tutorials--querying data, editing data, deleting data

Source: Internet
Author: User
Tags jqgrid

Querying DataUnder the ProcessRequest function, modify:Public void ProcessRequest (HttpContext context){//Findif (context. Request.Params.Get ("_search") = = "true"){return;}NameValueCollection forms = Context. Request.Form;string stroperation = forms. Get ("Oper");string strresponse = string. Empty;if (stroperation = = null){//Sortif (context. Request.Params.Get ("Sidx")! = null &&!string. IsNullOrEmpty (context. Request.Params.Get ("Sidx"). ToString ()) &&context. Request.Params.Get ("Sord")! = null &&!string. IsNullOrEmpty (context. Request.Params.Get ("Sord"). ToString ())){return;}strresponse = Getjson (Getuserdt ());//load Data}context. Response.Write (strresponse);}private DataTable Getuserdt (){string cmdtext = "Select UserId, Usercode, Password from T_user";SQLHelper SQLHelper = new SQLHelper ();DataTable dt = SqlHelper. Selectinfo (cmdtext);return DT;}private String Getjson (DataTable dt){JavaScriptSerializer serializer = new JavaScriptSerializer ();list<dictionary<string, object>> rows = new list<dictionary<string,object>> ();dictionary<string, object> row = null;foreach (DataRow dr in Dt. Rows){row = new dictionary<string, object> ();foreach (DataColumn col in dt. Columns){string s = Dr[col]. ToString (); Special Casesif (Col. ColumnName = = "ISAdmin"){s = s = = "1"? "Yes": "No";}if (Col. ColumnName = = "Haswrite"){s = s = = "1"? "Yes": "No";}row. Add (Col. Columnname.trim (), s);}rows. ADD (row);}return serializer. Serialize (rows);}A little attention can be found in the context. There are a number of parameters in the Request.params, such as the first time the load is as follows: {_search=false&nd=1410940615486&rows=10&page=1&sidx=&sord=asc&cnzzdata155540=cnzz_ eid%2 53d900936271-1409219704-%2526ntime%253d1409219704&all_http=http_connection%3akeepalive% 0d%0ahttp_accept%3aapplication%2fjson%2c+text%2fjavascript%2c+*%2f*%3b+q%3d0.01%0d%0ah ttp_accept_encoding%3agzip%2cdeflate%2csdch%0d%0ahttp_accept_language%3azh- cn%2czh%3bq%3d0.8%2czh- tw%3bq%3d0.6%0d%0ahttp_cookie%3acnzzdata155540%3dcnzz_eid%253d900936271-1409219704- %2526ntime%253d1409219704%0d%0ahttp_host%3alocalhost%3a12590%0d%0ahttp_referer%3ahttp%3a%2f% 2flocalhost%3a12590%2fjqgrid2.aspx%0d%0ahttp_user_agent%3amozilla%2f5.0+ (windows+nt+6.1) +App lewebkit%2f537.36+ (Khtml%2c+like+gecko) +chrome%2f37.0.2062.120+safari%2f537.36%0d%0ahttp_x_r equested_with%3axmlhttprequest%0d%0a&all_raw=connection%3a+keepalive% 0d%0aaccept%3a+application%2fjson%2c+text%2fjavascript%2c+*%2f*%3b+q%3d0.01%0d%0aaccep t-encoding%3a+gzip%2cdeflate%2csdch%0d%0aaccept-language%3a+zh-cn%2czh%3bq%3d0.8%2czh- tw%3bq%3d0.6%0d%0acookie%3a+cnzzdata155540%3dcnzz_eid%253d900936271-1409219704- %2526ntime%253d1409219704%0d%0ahost%3a+localhost%3a12590%0d%0areferer%3a+http%3a%2f%2flocalh ost%3a12590%2fjqgrid2.aspx%0d%0auser- agent%3a+mozilla%2f5.0+ (windows+nt+6.1) +applewebkit%2f537.36+ (Khtml%2c+like+gecko) +Chrome%2f 37.0.2062.120+safari%2f537.36%0d%0ax-requested- with%3a+xmlhttprequest%0d%0a&appl_md_path=%2flm%2fw3svc%2f17%2froot&appl_physical_path=c%3a% 5cusers%5cjhl%5cdocuments%5cvisual+studio+2013%5cprojects%5cjqgrid%5cjqgrid%5c&auth_type=&au Th_user=&auth_password=&logon_user=&remote_user=&cert_cookie=&cert_flags=&cert_ Issuer=&cert_ keysize=&cert_secretkeysize=&cert_serialnumber=&cert_server_issuer=&cert_server_subject= &cer T_subject=&content_length=0&content_type=&gateway_interface=cgi%2f1.1&https=off&https_ keysize=&https_secretkeysize=&https_server_issuer=&https_server_subject=&instance_id=17& Instance_m Eta_path=%2flm%2fw3svc%2f17&local_addr=%3a%3a1&path_info=%2fwebservice%2fuserhandler.ashx&pa th_translated=c%3a%5cusers%5cjhl%5cdocuments%5cvisual+studio+2013%5cprojects%5cjqgrid%5cjqgr id%5cwebservice%5cuserhandler.ashx&query_string=_search%3dfalse%26nd%3d1410940615486%26rows% 3d10%26page%3d1%26sidx%3d%26sord%3dasc&remote_addr=%3a%3a1&remote_host=%3a%3a1&remote_port=1 1798&request_method=get&script_name=%2fwebservice%2fuserhandler.ashx&server_name=localhost &s erver_port=12590&server_port_secure=0&server_protocol=http%2f1.1&server_software=microsoft- iis%2f8.0&url=%2fwebservice%2fuserhandler.ashx&http_connection=keepalive& http_accept=application%2fjson%2c+text%2fjavascript%2c+*%2f*%3b+q%3d0.01&http_accept_e ncoding=gzip%2cdeflate%2csdch&http_accept_language=zh-cn%2czh%3bq%3d0.8%2czh- tw%3bq%3d0.6&http_cookie=cnzzdata155540%3dcnzz_eid%253d900936271-1409219704- %2526ntime%253d1409219704&http_host=localhost%3a12590&http_referer=http%3a%2f%2flocalhost%3a 12590%2fjqgrid2.aspx&http_user_agent=mozilla%2f5.0+ (windows+nt+6.1) +applewebkit%2f537.36+ (KH Tml%2c+like+gecko) +chrome%2f37.0.2062.120+safari%2f537.36&http_x_requested_with=xmlhttpreque St} System.Collections.Specialized.NameValueCollection {System.Web.HttpValueCollection}When searching _search=true and adding or deleting, there are parameter oper. The first time you load it, you can see that this parameter is null. The data will be loaded at this point. Query out a DataTable serialized after the return is good, the front-end self-processing. Add when Oper=add modify Oper=edit delete when Oper=del data loading is completed there is an event loadcomplete, where you can do some things such as traversing the table to modify some of the display. For example, iterate through all the lines and change the usercode of each line to the line number:loadcomplete:function (XHR) {var users = $ (grid_selector). Jqgrid (' Getdataids ');if (users) {for (var i = 0, j = users.length; I < J; i++) {Parkid = $ (grid_selector). Jqgrid (' Getcell ', users[i],' Usercode ');$ (grid_selector). Jqgrid (' Setrowdata ', users[i], {' Usercode ':i});}}}, Edit DataNeed to add a column in the Colnames, the value of a custom or empty, colnames: [', ' Id ', ' username ', ' password '], corresponding, Colmodel need to add these content:{name: ' Myac ', Index: ', width:80, Fixed:true, sortable:false, Resize:false,formatter: ' Actions ',formatoptions: {Keys:true,//editbutton:true,Editformbutton:true,//Extraparam: {oper: ' edit '},editoptions: {//Edit operation, this is important to implement the editing of the transfer parametersanything. Reloadaftersubmit:true,//editdata: {//Editkey:function () {//var sel_id =$ (grid_selector). Jqgrid (' Getgridparam ', ' selrow ');//var value = $ (grid_selector). Jqgrid (' Getcell ',sel_id, ' UserId ');//return value;// }//},Closeafteredit:true,aftersubmit:function (response, postdata) {if (response.responsetext! = "") {alert (response.responsetext);$ (this). Jqgrid (' Setgridparam ', {datatype:' json '}). Trigger (' Reloadgrid ');return [True, Response.responsetext]}},},deloptions: {//delete operation, this is very important, implement delete when the transfer parameters. There are no examples of this online. Reloadaftersubmit:true,Deldata: {delid:function () {var sel_id =$ (grid_selector). Jqgrid (' Getgridparam ', ' selrow ');var value = $ (grid_selector). Jqgrid (' Getcell ',sel_id, ' UserId ');return value;}},aftersubmit:function (response, postdata) {if (response.responsetext! = "") {alert (response.responsetext);$ (this). Jqgrid (' Setgridparam ', {datatype:' json '}). Trigger (' Reloadgrid ');return [True, Response.responsetext]}}},}},Formatoptions with Delbutton and Editbutton, can be set to TRUE or false. The default is true. Generally do not care just fine. Width can only be a number that cannot be a percentage, and the grid calculates its own scale based on all columns. Fixed means that it does not scroll along with the following scroll axes. Sortable indicates whether sorting is possible. Editformbutton indicates whether to edit in a pop-up window or edit a row. Editoptions and Deloptions are some of the parameters that are set when editing or deleting. Reloadaftersubmit If the table data is reloaded after the operation completes, the general data changes must be reloaded once: $ (this). Jqgrid (' Setgridparam ', {datatype: ' json '}). Trigger (' Reloadgrid '); Closeafteredit closes the edit box when it is edited for a popup form. EditData and Deldata are important, where you can pass some parameters to the HTTP handler, such as getting the row ID to the handler function: var sel_id = $ (grid_selector). Jqgrid (' Getgridparam ', ' Selrow '); var value = $ (grid_selector). Jqgrid (' Getcell ', sel_id, ' UserID '); The above two lines of code implement the ' UserID ' that gets the selected row. Multiple choices need to traverse:var sel_id = [];sel_id = $ (grid_selector). Jqgrid (' Getgridparam ',' Selarrrow ');var value = ';For (var i = 0; i < sel_id.length; i++) {value = value + ', ' + $ (grid_selector). Jqgrid (' Getcell ',Sel_id[i], ' parkid ');}if (value.charat (0) = = ', ') {value = value.substr (1);}HTTP Handler handlers:if (stroperation = = "edit"){user. UserId = Int. Parse (forms. Get ("UserId"). ToString ());if (checkuserexist (user. Usercode, user. Userid.tostring ())){strresponse = "Duplicate user name, please confirm!" ";}Else{strresponse = UpdateUser (user)? "User update is successful!" ":" User update failed,Please confirm! ";}context. Response.Write (strresponse);}// <summary>// Update user Information// </summary>/// <param name= "objuser" ></param>/// <returns> update Success </returns>private bool UpdateUser (User objuser){String sql = "UPDATE t_user SET usercode = '" + objuser. Usercode +"', Password = '" + objuser. Password + "' WHERE userid=" + objuser. UserId;SQLHelper SQLHelper = new SQLHelper ();return SqlHelper. Adddelupdate (SQL) > 0;}private bool Checkuserexist (string usercode, string UserId){String sql = "SELECT * from t_user where usercode = '" + usercode + "' anduserid <> "+ userid;SQLHelper SQLHelper = new SQLHelper ();DataTable dt = SqlHelper. Selectinfo (SQL);return dt = = NULL | | (dt! = null && dt. Rows.Count = = 1);} Delete DataCode reference Edit data. HTTP Handler handlers:if (stroperation = = "Del"){strresponse = DeleteUser (forms. Get ("Delid"). ToString ())? "Delete succeeded!" " :"Delete failed, please confirm!" ";}context. Response.Write (strresponse);private bool DeleteUser (string userId){BOOL flag = true;string[] idlist = Userid.split (', ');foreach (var sid in Idlist){String sql = "DELETE from T_user WHERE userid=" + UserId;SQLHelper SQLHelper = new SQLHelper ();flag = SqlHelper. Adddelupdate (SQL) > 0;}return flag;}

Qgrid Series Tutorials--querying data, editing data, deleting data

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.