I am using ASP.Net (3.5 framework) in the Visual Studio 2010 environment for this example. Ultimately I am figuring out how to really make my sites pop visually and I think jQuery is the way to go. I am working on a project where I needed to replicate a large dataset that was bound to a desktop datagrid. Since this project is web based I wanted to see how I can do it in the web without using all the lame Microsoft controls. I will explain my position about this in another post. With a little searching I found a jQuery datagrid plugin here. It is called jqGrid.
Now… I spent days to be honest figuring out how to use this. However, I finally created a super simple example that worked which allowed me to use jqGrid in a much more complicated dataset on my project.
First the Basics:
You need the latest version of jQuery. You can find that here. You will also need the jQueryUI stylesheet. You can find that here. I suppose you don’t really need the jquery-ui-1.8.2.custom.css file but for just getting started it will make your life easier.
The Setup:
I’m using a MasterPage setup. So I’m putting all my needed files in my MasterPage and then I don’t have to add them to any of the other pages as long as they reference this MasterPage.
1 2 3 4 5 6 7 8 9
| <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<link href="Styles/jquery-ui-1.8.2.custom.css" rel="stylesheet" type="text/css" />
<link href="Styles/ui.jqgrid.css" rel="stylesheet" type="text/css" />
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="Scripts/grid.locale-en.js" type="text/javascript"></script>
<script src="Scripts/jquery.jqGrid.min.js" type="text/javascript"></script>
<script src="Scripts/jsTest1.js" type="text/javascript"></script> |
As you can see I have the jquery-1.4.1.min.js and the jquery-ui-1.8.2.custom.css as I mentioned above. The site.css file is the template I used for my site. There is an installation text file in the download. If you follow that then you will have access to jqGrid on your site. jsTest1.js is my javascript file that I use to make my jQuery calls.
The Object(s):
I am doing a very simple example but this will allow you to see the grid in action.
I have one object that has the following properties:
1 2 3 4 5
| Public Class myTest
Private _total As Integer
Private _records As Integer
Private _page As Integer
Private _rows As List(of names) |
Then I have a “names” object with the following properties:
1 2 3
| Public Class names
Private _name As String
Private _title As String |
As you can see this was a very simple example. There may be other ways to set this up but this is how I chose to do it. Which worked, which was most important at the time.
The WebService:
If you have not checked out web services in .Net you should really spend some time with them. I am using them all the time when doing AJAX calls. jQuery and webservices now go hand in hand. I will provide an example of how to call a webservice from jQuery but more detail will be in another post.
When you first create a webservice file in ASP.Net it looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class test
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function HelloWorld() As String
Return "Hello World"
End Function
End Class |
In order to use the webservice with jQuery and jqGrid then you need add a few things. The following is the webservice that I used for my example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Web
Imports System.Web.Script.Services
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class myTestWS
Inherits System.Web.Services.WebService
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
<WebMethod()> _
Public Function testMethod() As myTest
Dim myString As New myTest
myString.records = 2
myString.total = 2
myString.page = 1
Dim myName As New names
myName.name = "Jeff"
myName.title = "Programmer"
Dim myNamesList As New List(Of names)
myNamesList.Add(myName)
myString.rows = myNamesList
Dim myName2 As New names
myName2.name = "Steve"
myName2.title = "Programmer"
myNamesList.Add(myName2)
Return myString
End Function
End Class |
As you compare the differences between the original and the one I used I added:
Imports System.Web
Imports System.Web.Script.Services
I also uncommented out the following line (just followed Microsofts instructions):
<System.Web.Script.Services.ScriptService()>
Then I also added the following above my <WebMethod()> _ call:
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
I haven’t done a lot of research as to why I needed all that, I was just getting my example to work. However, as I do more of these using webservices and jQuery I will probably have to figure it out. More to come on that.
As you can see in my webservice method I am just populating the properties and adding data to the list property.
Now the jQuery:
In my jsTest1.js file (mentioned above) I have the code that calls the webservice using the jQuery .ajax function returning JSON data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| function processrequest(postdata) {
$(".loading").show();
$.ajax({
type: "POST",
data: "{}",
datatype: "json",
url: "../webServices/myTestWS.asmx/testMethod",
contentType: "application/json; charset-utf-8",
complete: function (jsondata, stat) {
if (stat == "success") {
var thegrid = jQuery("#list")[0];
var jsonObject = (eval("(" + jsondata.responseText + ")"));
thegrid.addJSONData(jsonObject.d);
$(".loading").hide();
} else {
$(".loading").hide();
alert("Error with AJAX callback");
}
}
});
} |
In lines 3-20: is your .ajax call. Go here for detailed information.
I use firebug for FireFox to help me in my jQuery exploits. I was able to get the JSON data returned from the webservice using this tool. The JSON data looks like this:
1
| {"d":{"__type":"myJQueryTestBed.myTest","total":2,"records":2,"page":1,"rows":[{"name":"Jeff","title"<code>:"Programmer"},{"name":"Steve","title":"Programmer"}]}} |
The actual jQuery code is here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| jQuery(document).ready(function () {
jQuery("#list").jqGrid({
datatype: processrequest,
mtype: 'POST',
jsonReader: {
root: "rows", //arry containing actual data
page: "page", //current page
total: "total", //total pages for the query
records: "records", //total number of records
repeatitems: false,
id: "ID" //index of the column with the PK in it
},
colNames: ['Name', 'Title'],
colModel: [
{ name: 'name', index: 'name', width: 55 },
{ name: 'title', index: 'title', width: 90 }
],
pager: jQuery('#pager'),
rowNum: 10,
rowList: [10, 20, 30],
sortorder: "desc",
viewrecords: true,
caption: 'My first grid'
});
}); |
Notice line 3: datatype: processrequest,
This line calls the function processrequest which called the webservice. You could see examples that make the Ajax call inside the jQuery function above but I found it was a little cleaner for me to separate out the calls.
The most obscure item are lines 5-12: the jsonrReader. I'm still trying to figure it all out but it takes your JSON string and uses it in the jqGrid. I will tell you once I got my data from the webservice in the right format and added the JSONReader block of code the grid worked.
As you can see the JSON data returned from my webservice includes the names that the JSONReader code is looking for. In all my searches online on how to figure this out the people that seemed to know what they were talking about all said you need this block. However, the documentation on this I found to be hard to understand as I'm still trying to figure it out.
Well if you put all of this together I got the following to finally work:

Overall, I'm disappointed it took me a couple of days to figure out this grid. However, maybe it is a little bit of me (being new to jQuery and webservices and JSON) and a little bit of the plugin being that it is so robust it makes it complicated. I'm still figuring out the application, but I'm hopeful everything else will be a lot easier now that I have data in the grid.