How do I get jqGrid to work using ASP.NET + JSON on the backend?

45,311

Solution 1

Usually when you get the 'blah not a function' error with jqGrid it's because the correct module has not been loaded. The addJSONData function is defined in the grid.base.js file. Can you check your jqGridInclude() function in the jquery.jqGrid.js file and make sure that grid.base.js is being included as part of the initialization of your modules variable?

Solution 2

Have you verified the tbl variable is getting reference to your jqgrid instance ?

Try adding an id to your table element and get the reference to the jqgrid like:

var thegrid = jQuery("#mytableid")[0];

Solution 3

maybe some help in this code here posted on stack overflow? jqgrid with asp.net webmethod and json working with sorting, paging, searching and LINQ -- but needs dynamic operators

Solution 4

well i see one thing wrong here:

var tbl = jQuery('table.scroll')[0];  
 //tbl.addJSONData(objGridData); //error received: addJSONData not a function  

if you are indeed wondering why you are getting this error, it is because tbl does not have that function.

Edit: i got curious, and checked if jqGrid added those methods to the DOM reference object. and they did. (i checked using firebug here: http://trirand.com/jqgrid/jqgrid.html).

One thing that may be happening here is that you have multiple tables of class 'scroll' and your jquery is returning the wrong one.

Share:
45,311
Briana Finney
Author by

Briana Finney

I work hard and play hard; I absolutely love everything about software development...well, most of it. I don't like to be micro-managed; hell, I barely like to be managed ;-) . I love to laugh, have a dry, sarcastic sense of humor and a lot of folks really don't know how to take me. My Meyer's Briggs Type Indicator is an INTP, which means, I'm pretty similar to about 2.9% of the world's population of women. As if women weren't hard enough as it is, right? To sum it up, a colleague of mine, and good friend, told me a couple years ago, "You're just like a dude! Only better!". I'm still not sure what he meant exactly, but I took it as a compliment. I'm too young to not be still moving fast, and I'm too old to be moving so fast that I miss it. I'm having the time of my life these days..and I hope you are too! Enjoy a cool one and summer sunshine!

Updated on July 09, 2022

Comments

  • Briana Finney
    Briana Finney almost 2 years

    ok, I'm back. I totally simplified my problem to just three simple fields and I'm still stuck on the same line using the addJSONData method. I've been stuck on this for days and no matter how I rework the ajax call, the json string, blah blah blah...I can NOT get this to work! I can't even get it to work as a function when adding one row of data manually. Can anyone PLEASE post a working sample of jqGrid that works with ASP.NET and JSON? Would you please include 2-3 fields (string, integer and date preferably?) I would be happy to see a working sample of jqGrid and just the manual addition of a JSON object using the addJSONData method. Thanks SO MUCH!! If I ever get this working, I will post a full code sample for all the other posting for help from ASP.NET, JSON users stuck on this as well. Again. THANKS!!

    tbl.addJSONData(objGridData); //err: tbl.addJSONData is not a function!!

    Here is what Firebug is showing when I receive this message:

    • objGridData Object total=1 page=1 records=5 rows=[5]
    ○ Page "1"
    Records "5"
    Total "1"
    Rows [Object ID=1 PartnerID=BCN, Object ID=2 PartnerID=BCN, Object ID=3 PartnerID=BCN, 2 more... 0=Object 1=Object 2=Object 3=Object 4=Object]
    (index) 0
    (prop) ID (value) 1 (prop) PartnerID (value) "BCN" (prop) DateTimeInserted (value) Thu May 29 2008 12:08:45 GMT-0700 (Pacific Daylight Time)
    * There are three more rows

    Here is the value of the variable tbl (value) 'Table.scroll'

    <TABLE cellspacing="0" cellpadding="0" border="0" style="width: 245px;" class="scroll grid_htable"><THEAD><TR><TH class="grid_sort grid_resize" style="width: 55px;"><SPAN> </SPAN><DIV id="jqgh_ID" style="cursor: pointer;">ID <IMG src="http://localhost/DNN5/js/jQuery/jqGrid-3.4.3/themes/sand/images/sort_desc.gif"/></DIV></TH><TH class="grid_resize" style="width: 90px;"><SPAN> </SPAN><DIV id="jqgh_PartnerID" style="cursor: pointer;">PartnerID </DIV></TH><TH class="grid_resize" style="width: 100px;"><SPAN> </SPAN><DIV id="jqgh_DateTimeInserted" style="cursor: pointer;">DateTimeInserted </DIV></TH></TR></THEAD></TABLE>
    

    Here is the complete function:

     $('table.scroll').jqGrid({  
        datatype: function(postdata) {    
            mtype: "POST",    
        $.ajax({  
            url: 'EDI.asmx/GetTestJSONString',  
            type: "POST",  
            contentType: "application/json; charset=utf-8",  
            data: "{}",  
            dataType: "text", //not json . let me try to parse  
            success: function(msg, st) {  
                if (st == "success") {                    
                     var gridData;  
    
                    //strip of "d:" notation  
                    var result = JSON.parse(msg);   
                    for (var property in result) {  
                        gridData = result[property];  
                        break;  
                    }  
    
                    var objGridData = eval("(" + gridData + ")"); //creates an object with visible data and structure  
                    var tbl = jQuery('table.scroll')[0];  
    
                    alert(objGridData.rows[0].PartnerID); //displays the correct data  
    
                    //tbl.addJSONData(objGridData); //error received: addJSONData not a function  
    
                    //error received: addJSONData not a function (This uses eval as shown in the documentation)  
                    //tbl.addJSONData(eval("(" + objGridData + ")"));   
    
                    //the line below evaluates fine, creating an object and visible data and structure  
                    //var objGridData = eval("(" + gridData + ")");  
                    //BUT, the same thing will not work here  
                    //tbl.addJSONData(eval("(" + gridData + ")"));  
                    //FIREBUG SHOWS THIS AS THE VALUE OF gridData:  
                   // "{"total":"1","page":"1","records":"5","rows":[{"ID":1,"PartnerID":"BCN","DateTimeInserted":new Date(1214412777787)},{"ID":2,"PartnerID":"BCN","DateTimeInserted":new Date(1212088125000)},{"ID":3,"PartnerID":"BCN","DateTimeInserted":new Date(1212088125547)},{"ID":4,"PartnerID":"EHG","DateTimeInserted":new Date(1235603192033)},{"ID":5,"PartnerID":"EMDEON","DateTimeInserted":new Date(1235603192000)}]}"         
    
                }  
            }  
        });  
        },  
        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: [  
            'ID', 'PartnerID', 'DateTimeInserted'  
              ],    
        colModel: [    
        { name: 'ID', index: 'ID', width: 55 },    
        { name: 'PartnerID', index: 'PartnerID', width: 90 },  
        { name: 'DateTimeInserted', index: 'DateTimeInserted', width: 100}],  
        rowNum: 10,  
        rowList: [10, 20, 30],  
        imgpath: 'http://localhost/DNN5/js/jQuery/jqGrid-3.4.3/themes/sand/images',  
        pager: jQuery('#pager'),  
        sortname: 'ID',  
        viewrecords: true,  
        sortorder: "desc",  
       caption: "TEST Example")};  
    
  • Briana Finney
    Briana Finney about 15 years
    I have verified that the tbl variable is evaluating properly and i only have one instance. Any other suggestions?? Anything at all is welcome! :-) Thanks!
  • mkoryak
    mkoryak about 15 years
    i am going home now, if you are still stuck send me an email at [email protected] and maybe i can help you out tonight. this should be an easy fix
  • Marko
    Marko over 14 years
    yes, I have the same issue,everything is fine, I get proper data but I can't load data in grid..anyone solved this?
  • fletchsod
    fletchsod almost 11 years
    As of today, unfortunately, the WebForm is dying out as everyone is moving to javascript framework.