Hello Friends,
I have seen over the internet that there are many articles of simple jqGrid. In this article, I've tried to show you the use of jqGrid's functionality (eg. searching, sorting, paging, filtering, checkboxes, etc) and its states after a postback request. First question is what is jqgrid?
What is JqGrid?
  • JqGrid is Stylish and Featured Tabular data presentation control.
  • JqGrid is a JavaScript control for representing and manipulating tabular data on the web.
  • JqGrid is Ajax Enabled.
  • JqGrid Can be integrated with any of the server-side technologies like ASP, JavaServelets, JSP, PHP etc.
  • JqGrid was developed by Tony Tomov at Trirand Inc.
  • JqGrid is very simple to integrate with ASP.NET.
You can download all the scripts and style sheet file from this link https://github.com/jqwidgets/jQWidgets/tree/master/jqwidgets
or you can find the jqwidget folder from attached demo project also.
I have used simple static data in this demo, but you can use data from the database here. You can see the project structure as given below.

ASP.NET
Image1
The following is the test data function of "SimplePage.aspx.cs" page.
  1. [WebMethod]
  2. public static string GetProductsList()
  3. {
  4. List<ResponseMessage> respmsg = new List<ResponseMessage>();
  5. respmsg.Add(new ResponseMessage { ProID = 1,ID = 1, Name = "Product1", SKU = "sku1", Price = 100, vendorname = "Nike", Active = true });
  6. respmsg.Add(new ResponseMessage { ProID = 2,ID = 2, Name = "Product2", SKU = "sku2", Price = 100, vendorname = "Nike", Active = false });
  7. respmsg.Add(new ResponseMessage { ProID = 3,ID = 3, Name = "Product3", SKU = "sku3", Price = 100, vendorname = "Nike", Active = true });
  8. respmsg.Add(new ResponseMessage { ProID = 4, ID = 4, Name = "Product4", SKU = "sku4", Price = 100, vendorname = "Nike", Active = true });
  9. respmsg.Add(new ResponseMessage { ProID = 5, ID = 5, Name = "Product5", SKU = "sku5", Price = 100, vendorname = "Nike", Active = true });
  10. respmsg.Add(new ResponseMessage { ProID = 6, ID = 6, Name = "Product6", SKU = "sku6", Price = 100, vendorname = "Nike", Active = false });
  11. respmsg.Add(new ResponseMessage { ProID = 7, ID = 7, Name = "Product7", SKU = "sku7", Price = 100, vendorname = "Nike", Active = false });
  12. respmsg.Add(new ResponseMessage { ProID = 8, ID = 8, Name = "Product8", SKU = "sku8", Price = 100, vendorname = "Nike", Active = true });
  13. respmsg.Add(new ResponseMessage { ProID = 9, ID = 9, Name = "Product9", SKU = "sku9", Price = 100, vendorname = "Nike", Active = true });
  14. respmsg.Add(new ResponseMessage { ProID = 10, ID = 10, Name = "Product10", SKU = "sku10", Price = 100, vendorname = "Nike", Active = true });
  15. respmsg.Add(new ResponseMessage { ProID = 11, ID = 11, Name = "Product11", SKU = "sku11", Price = 100, vendorname = "Nike", Active = true });
  16. respmsg.Add(new ResponseMessage { ProID = 12, ID = 12, Name = "Product12", SKU = "sku12", Price = 100, vendorname = "Nike", Active = true });
  17. respmsg.Add(new ResponseMessage { ProID = 13, ID = 13, Name = "Product13", SKU = "sku13", Price = 100, vendorname = "Nike", Active = true });
  18. respmsg.Add(new ResponseMessage { ProID = 14, ID = 14, Name = "Product14", SKU = "sku14", Price = 100, vendorname = "Nike", Active = true });
  19. respmsg.Add(new ResponseMessage { ProID = 15, ID = 15, Name = "Product15", SKU = "sku15", Price = 100, vendorname = "Nike", Active = true });
  20. respmsg.Add(new ResponseMessage { ProID = 16, ID = 16, Name = "Product16", SKU = "sku16", Price = 100, vendorname = "Nike", Active = true });
  21. respmsg.Add(new ResponseMessage { ProID = 17, ID = 17, Name = "Product17", SKU = "sku17", Price = 100, vendorname = "Nike", Active = false });
  22. respmsg.Add(new ResponseMessage { ProID = 18, ID = 18, Name = "Product18", SKU = "sku18", Price = 100, vendorname = "Nike", Active = true });
  23. respmsg.Add(new ResponseMessage { ProID = 19, ID = 19, Name = "Product19", SKU = "sku19", Price = 100, vendorname = "Nike", Active = false });
  24. respmsg.Add(new ResponseMessage { ProID = 20, ID = 20, Name = "Product20", SKU = "sku20", Price = 100, vendorname = "Nike", Active = false });
  25. respmsg.Add(new ResponseMessage { ProID = 21, ID = 21, Name = "Product21", SKU = "sku21", Price = 100, vendorname = "Nike", Active = false });
  26. return Newtonsoft.Json.JsonConvert.SerializeObject(respmsg);
  27. }
  28. public class ResponseMessage
  29. {
  30. public int ProID { get; set; }
  31. public int ID { get; set; }
  32. public string Name { get; set; }
  33. public string SKU { get; set; }
  34. public float Price { get; set; }
  35. public string vendorname { get; set; }
  36. public bool Active { get; set; }
  37. }
The following is jqGrid.aspx code.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="JqGrid.aspx.cs" Inherits="JqGrid" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>JQGrid Demo - Searching, Sorting, Filtering, Paging, Checkbox Selection, etc</title>
  6. <link href="css/jqx.base.css" rel="stylesheet" />
  7. <script src="Js/jquery-3.2.1.min.js"></script>
  8. <link href="css/custom.css" rel="stylesheet" />
  9. <link href="css/font-awesome.min.css" rel="stylesheet" />
  10. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  11. <script type="text/javascript">
  12. $(document).ready(function () {
  13. $.ajax({
  14. type: "POST",
  15. url: "SimplePage.aspx/GetProductsList",
  16. contentType: "application/json; charset=utf-8",
  17. dataType: "json",
  18. data: "",
  19. async: false,
  20. success: function (data) {
  21. var obj = data.d;
  22. var source =
  23. {
  24. datafields: [
  25. { name: 'ID', type: 'int' },
  26. { name: 'ProID', type: 'int' },
  27. { name: 'Name', type: 'string' },
  28. { name: 'SKU', type: 'string' },
  29. { name: 'Price', type: 'float' },
  30. { name: 'vendorname', type: 'string' },
  31. { name: 'Active', type: 'bool' },
  32. ],
  33. id: 'ID',
  34. datatype: "json",
  35. localdata: obj
  36. };
  37. var dataList = new $.jqx.dataAdapter(source, { contentType: 'application/json; charset=utf-8' });
  38. var viewStatus = function (rownumber, columnname, value) {
  39. if (value == false) {
  40. var cc = '<i class="fa fa-2x fa-thumbs-down text-danger"></i>'
  41. }
  42. else {
  43. var cc = '<i class="fa fa-2x fa-thumbs-up text-success"></i>'
  44. }
  45. return '<div style="position: relative; left: 50%; top: 50%; transform: translate(-50%,-50%); display: inline-block;">' + cc + '</div>';
  46. }
  47. var viewAction = function (rownumber, columnname, value) {
  48. return '<div style="position: relative; left: 50%; top: 50%; transform: translate(-50%,-50%); display: inline-block; text-align: center;"><a href="javascript:void(0)" title="" onclick="EditProduct(' + value + ')" class="btn btn-success btn-sm"><i class="fa fa-edit"></i> Edit Product</a></div>';
  49. }
  50. $("#grid").jqxGrid(
  51. {
  52. width: '60%',
  53. height: 365,
  54. altrows: true,
  55. source: dataList,
  56. pageable: true,
  57. pagesize: 5,
  58. pagesizeoptions: ["5", "10", "15"],
  59. autoheight: true,
  60. autorowheight: true,
  61. columnsheight: 70,
  62. selectionmode: 'checkbox',
  63. showfilterrow: true,
  64. filterable: true,
  65. sortable: true,
  66. ready: function () { },
  67. columns: [
  68. { text: 'ID', datafield: 'ID', width: '8%', align: 'center' },
  69. { text: 'Product Name', datafield: 'Name', width: '25%', align: 'left' },
  70. { text: 'SKU', datafield: 'SKU', width: '15%' },
  71. { text: 'Vendor Name', datafield: 'vendorname', width: '16%', align: 'center' },
  72. { text: 'Price', datafield: 'Price', width: '11%', align: 'center', cellsalign: 'right', cellsformat: 'c2' },
  73. { text: 'Status', datafield: 'Active', width: '8%', cellsrenderer: viewStatus, align: 'center', filtertype: 'bool', sortable: false },
  74. { text: 'Actions', datafield: 'ProID', width: '14%', cellsrenderer: viewAction, align: 'center', filterable: false, sortable: false, }
  75. ]
  76. });
  77. //=================================
  78. if ('<%=Session["sessionTextBox"] %>' != null || '<%=Session["sessionCheckbox"] %>' != null || '<%=Session["sessionPageNumber"] %>' != null || '<%=Session["sessionPageRowCount"] %>' != null || '<%=Session["sessionColumnSorting"] %>' != null) {
  79. textboxValue = '<%=Session["sessionTextBox"] %>';
  80. gridCheckboxOption = '<%=Session["sessionCheckbox"] %>';
  81. gridPageNumber = '<%=Session["sessionPageNumber"] %>';
  82. gridPageRowCount = '<%=Session["sessionPageRowCount"] %>';
  83. gridColumnSorting = '<%=Session["sessionColumnSorting"] %>';
  84. $.ajax(
  85. {
  86. type: "POST",
  87. url: "JqGrid.aspx/DestroySession",
  88. contentType: "application/json; charset=utf-8",
  89. data: "",
  90. dataType: "json",
  91. async: false,
  92. cache: "false",
  93. success: function (data) {
  94. }, Error: function (x, e) {
  95. alert("Error in Delete");
  96. }
  97. });
  98. }
  99. //paging row counts
  100. if (gridPageRowCount > 5) {
  101. $($('#grid').find('div[id="gridpagerlistgrid"]')[0]).val(gridPageRowCount);
  102. }
  103. //Column Header Sorting
  104. var allgridsortingvalues;
  105. if (gridColumnSorting.includes('ascending') || gridColumnSorting.includes('descending')) {
  106. gridColumnSorting = gridColumnSorting.replace(/none/gi, 'undefined');
  107. allgridsortingvalues = gridColumnSorting.split('|');
  108. }
  109. else {
  110. gridColumnSorting = gridColumnSorting.replace(/none/gi, 'undefined');
  111. allgridsortingvalues = gridColumnSorting.split('|');
  112. }
  113. var allsortingtext = $('#grid').find('div[role^="columnheader"]');
  114. var tcontrolsorting = null;
  115. if (allgridsortingvalues.length > 1) {
  116. for (var i = 1; i < allsortingtext.length; i++) {
  117. if (allgridsortingvalues[i].toString() != 'undefined') {
  118. while ($(allsortingtext[i]).attr('aria-sort') != allgridsortingvalues[i].toString()) {
  119. $('#grid').trigger('reloadGrid');
  120. $(allsortingtext[i]).attr('aria-sort', allgridsortingvalues[i].toString());
  121. tcontrolsorting = allsortingtext[i];
  122. $(tcontrolsorting).trigger('click');
  123. }
  124. break;
  125. }
  126. }
  127. }
  128. //Textboxes
  129. var allgridvalues = textboxValue.split('|');
  130. var alltext = $('#grid').find('input[id^="jqxWidget"]');
  131. var tcontrol = null;
  132. var checkBool = false;
  133. if (allgridvalues.length > 1) {
  134. for (var i = 0; i < alltext.length; i++) {
  135. if (allgridvalues[i].toString() != "") {
  136. $(alltext[i]).val(allgridvalues[i].toString());
  137. tcontrol = alltext[i];
  138. checkBool = true;
  139. }
  140. }
  141. if (checkBool == true) {
  142. $(tcontrol).trigger('keydown');
  143. }
  144. }
  145. //paging
  146. if (gridPageNumber > 1) {
  147. while ($($('#grid').find('input[class="jqx-input jqx-widget-content jqx-grid-pager-input jqx-rc-all"]')[0]).val() != gridPageNumber) {
  148. $('#grid').trigger('reloadGrid');
  149. $($('#grid').find('input[class="jqx-input jqx-widget-content jqx-grid-pager-input jqx-rc-all"]')[0]).val(gridPageNumber);
  150. $($('#grid').find('input[class="jqx-input jqx-widget-content jqx-grid-pager-input jqx-rc-all"]')[0]).trigger('change');
  151. }
  152. }
  153. //Checkboxes
  154. var allgridcheckboxvalues = gridCheckboxOption.split('|');
  155. if ((allgridcheckboxvalues.length) > 1) {
  156. var allcheckboxtext = $('#grid').find('div[class^="jqx-checkbox-default"]');
  157. var tcontrol1 = null;
  158. var tcontrol2 = null;
  159. var tcontrol3 = null;
  160. var ckhAllBool = true;
  161. for (var i = 0; i < allcheckboxtext.length; i++) {
  162. if (i == 0 && allgridcheckboxvalues[i].toString() != "") {
  163. tcontrol1 = allcheckboxtext[i];
  164. $(tcontrol1).find('span').attr('class', allgridcheckboxvalues[i].toString());
  165. if (allgridcheckboxvalues[i].toString() == "jqx-checkbox-check-checked") {
  166. ckhAllBool = false;
  167. }
  168. }
  169. else if (i == 1 && allgridcheckboxvalues[i].toString() != "") {
  170. tcontrol2 = allcheckboxtext[i];
  171. while ($(allcheckboxtext[i]).find('span').attr('class') != allgridcheckboxvalues[i].toString().replace('jqxcheckboxcheckindeterminate---', '').toString().trim().replace('jqxcheckboxcheckchecked---', '').toString().trim()) {
  172. $('#grid').trigger('reloadGrid');
  173. $(allcheckboxtext[i]).find('span').attr('class', allgridcheckboxvalues[i].toString());
  174. $(allcheckboxtext[i]).trigger('mousedown');
  175. }
  176. }
  177. else {
  178. if (ckhAllBool == true) {
  179. tcontrol3 = allcheckboxtext[i];
  180. $(tcontrol3).find('span').attr('class', allgridcheckboxvalues[i].toString());
  181. }
  182. else {
  183. tcontrol3 = allcheckboxtext[i];
  184. $(tcontrol3).find('span').attr('class', 'jqx-checkbox-check-checked');
  185. }
  186. }
  187. }
  188. if (tcontrol1 != null) {
  189. $(tcontrol1).trigger('keydown');
  190. }
  191. if (tcontrol3 != null) {
  192. $(tcontrol3).trigger('keydown');
  193. }
  194. }
  195. //=================================
  196. }
  197. });
  198. });
  199. var textboxValue = "";
  200. var gridCheckboxOption = "";
  201. var gridPageNumber = "";
  202. var gridPageRowCount = "";
  203. var gridColumnSorting = "";
  204. function EditProduct(val) {
  205. //pagenumber
  206. var strPageNumber = $($('#grid').find('input[class="jqx-input jqx-widget-content jqx-grid-pager-input jqx-rc-all"]')[0]).val();
  207. //pagenumber
  208. //pageRowCount
  209. var strPageRowCount = $($('#grid').find('div[id="gridpagerlistgrid"]')[0]).val();
  210. //pageRowCount
  211. //Column Header Sorting
  212. var allColumnSortHeader = $('#grid').find('div[role^="columnheader"]');
  213. for (var i = 0; i < allColumnSortHeader.length; i++) {
  214. if (i == 0) {
  215. gridColumnSorting = $(allColumnSortHeader[i]).attr('aria-sort') + "|";
  216. }
  217. else {
  218. gridColumnSorting = gridColumnSorting + $(allColumnSortHeader[i]).attr('aria-sort') + "|";
  219. }
  220. }
  221. //Column Header Sorting
  222. //Column textboxes
  223. var alltext = $('#grid').find('input[id^="jqxWidget"]');
  224. for (var i = 0; i < alltext.length; i++) {
  225. if (i == 0) {
  226. textboxValue = $(alltext[i]).val();
  227. }
  228. else {
  229. textboxValue = textboxValue + "|" + $(alltext[i]).val();
  230. }
  231. }
  232. //Column textboxes
  233. //Column checkboxes
  234. var alltext = $('#grid').find('div[class^="jqx-checkbox-default"]');
  235. var tcontrol = null;
  236. var tcontrol1 = null;
  237. for (var i = 0; i < alltext.length; i++) {
  238. if (i == 0 && alltext[i] != null) {
  239. tcontrol = alltext[i];
  240. gridCheckboxOption = $(tcontrol).find('span').attr("class");
  241. }
  242. //====For checkbox of STATUS column in grid
  243. else if (i == 1 && alltext[i] != null) {
  244. tcontrol1 = alltext[i];
  245. gridCheckboxOption = gridCheckboxOption + "|" + $(tcontrol1).find('span').attr("class");
  246. }
  247. //====For checkbox of STATUS column in grid
  248. else {
  249. tcontrol = alltext[i];
  250. gridCheckboxOption = gridCheckboxOption + "|" + $(tcontrol).find('span').attr("class");
  251. }
  252. }
  253. //Column checkboxes
  254. textboxValue = textboxValue.replace(/'/g, "%27").replace(/&/g, "%26").replace(/\//, "%2F").replace(/\?/g, "%3F");
  255. $.ajax(
  256. {
  257. type: "POST",
  258. url: "JqGrid.aspx/SetSearchReminderSessions",
  259. contentType: "application/json; charset=utf-8",
  260. data: "{ textboxValue:'" + textboxValue + "', gridCheckboxOption:'" + gridCheckboxOption + "', strPageNumber: '" + strPageNumber + "', strPageRowCount:'" + strPageRowCount + "', gridColumnSorting:'" + gridColumnSorting + "' }",
  261. dataType: "json",
  262. async: false,
  263. cache: "false",
  264. success: function (data) {
  265. if (data.d == "0") {
  266. window.location = "/SimplePage.aspx?Id=" + val;
  267. }
  268. }
  269. });
  270. }
  271. function Clearfilter() {
  272. $('#grid').jqxGrid('clearselection');
  273. $("#grid").jqxGrid('clearfilters');
  274. var allcheckboxtext = $('#grid').find('div[class^="jqx-checkbox-default"]');
  275. for (var i = 0; i < allcheckboxtext.length; i++) {
  276. $(allcheckboxtext[i]).find('span').attr('class', '-');
  277. }
  278. }
  279. </script>
  280. </head>
  281. <body>
  282. <form id="form1" runat="server">
  283. <div class="content">
  284. <div class="page-content">
  285. <div class="page-header position-relative">
  286. <h1>JQGrid Demo</h1>
  287. </div>
  288. </div>
  289. <div class="row">
  290. <div id="grid"></div>
  291. </div>
  292. </div>
  293. </form>
  294. <script type="text/javascript" src="Js/jqxcore.js"></script>
  295. <script type="text/javascript" src="Js/jqxdata.js"></script>
  296. <script type="text/javascript" src="Js/jqxbuttons.js"></script>
  297. <script type="text/javascript" src="Js/jqxscrollbar.js"></script>
  298. <script type="text/javascript" src="Js/jqxmenu.js"></script>
  299. <script type="text/javascript" src="Js/jqxgrid.js"></script>
  300. <script type="text/javascript" src="Js/jqxgrid.pager.js"></script>
  301. <script type="text/javascript" src="Js/jqxgrid.selection.js"></script>
  302. <script type="text/javascript" src="Js/jqxgrid.filter.js"></script>
  303. <script type="text/javascript" src="Js/jqxgrid.sort.js"></script>
  304. <script type="text/javascript" src="Js/jqxlistbox.js"></script>
  305. <script type="text/javascript" src="Js/jqxwindow.js"></script>
  306. <script type="text/javascript" src="Js/jqxdropdownlist.js"></script>
  307. <script type="text/javascript" src="Js/jqxcheckbox.js"></script>
  308. </body>
  309. </html>
The following is the jqGrid.aspx.cs code. You can set and clear/destroy the session for all the required controls of jqGrid.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Services;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. public partial class JqGrid : System.Web.UI.Page
  9. {
  10. protected void Page_Load(object sender, EventArgs e)
  11. {
  12. }
  13. [WebMethod]
  14. public static string SetSearchReminderSessions(string textboxValue, string gridCheckboxOption, string strPageNumber, string strPageRowCount, string gridColumnSorting)
  15. {
  16. HttpContext.Current.Session["sessionTextBox"] = System.Web.HttpContext.Current.Server.UrlDecode(textboxValue).Replace("'", @"\'");
  17. HttpContext.Current.Session["sessionCheckbox"] = gridCheckboxOption;
  18. HttpContext.Current.Session["sessionPageNumber"] = strPageNumber;
  19. HttpContext.Current.Session["sessionPageRowCount"] = strPageRowCount;
  20. HttpContext.Current.Session["sessionColumnSorting"] = gridColumnSorting;
  21. return "0";
  22. }
  23. [WebMethod]
  24. public static string DestroySession()
  25. {
  26. HttpContext.Current.Session["sessionTextBox"] = null;
  27. HttpContext.Current.Session["sessionCheckbox"] = null;
  28. HttpContext.Current.Session["sessionPageNumber"] = null;
  29. HttpContext.Current.Session["sessionPageRowCount"] = null;
  30. HttpContext.Current.Session["sessionColumnSorting"] = null;
  31. return "0";
  32. }
  33. }
Now, after downloading the source code, build it and then run the application; it will show you the jqGrid as shown below. I have set 5 records per page as shown on the screen.
CHECKBOX

ASP.NET
Image2

Now, I am going step by step to show you the function. Select all or specific product checkboxes as shown in the below screen and after that, click "EDIT PRODUCT" button which will call the post request.

ASP.NET
Image3

Here, after calling the Edit button of any selected product, you move to another page. You can do everything as per your project's requirement, but after coming back again, you need to maintain all the states of controls as they are. Because, once there are many records in jqGrid, it is very frustrating for the user to search and find all the selected products again.

ASP.NET
Image4

So, I will show you after coming back that the state of controls is saved as it is.

ASP.NET
Image5

SEARCHING

Now, in the same way as the above checkbox functionality, you can use searching, filtering, sorting and paging functionality. I will show you just images of it. You can run the demo and experience it for yourself.

ASP.NET
Image6

Filtering/Sorting

Here, you can see there are 3 types of sorting given by jqGrid -
1) Ascending
2) Descending
3) No sort.

ASP.NET
Image7

Status Filtering(ACTIVE)

Here, you can see that if you want to get products which are in an active status, then you can do the following.

ASP.NET
Image8

Status Filtering(IN-ACTIVE)

Here, you can see that if you want to get products which are in inactive status than, you can do this.

ASP.NET
Image9

Paging

Here, you can see that you can move to the third page with the use of paging functionality.

ASP.NET
Image10

Another Important thing to find while you are using jqGrid is that you need to find the event/trigger of the controls. So, I will show you a simple solution, which is to just use developer tool and click on the control for which you want to find the event. Now, go to event listener of that control and you will find event/trigger from there, as shown below.

ASP.NET
Image11

I hope you will like this article and may use it for your projects. Suggestions are welcome.

Thanks in advance!