Forum Replies Created

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • Thanks Peter, I just happened to resolve the issue before I saw your reply. I remembered spotting JavaScriptSerializer.DeserializeObject() in a few of your JSON examples. It appears I was returning JSON as a string – it looks like I have better luck returning it as an object. I can now see the data appearing on the grid.

    So where I had:
    return new JavaScriptSerializer().Serialize(records.Select(x => new { x.LastName, x.FirstName }));

    I should have had:
    string json = new JavaScriptSerializer().Serialize(records.Select(x => new { x.LastName, x.FirstName }));
    return new JavaScriptSerializer().DeserializeObject(json);

    Thank you for your time and suggestions.

    Cheers
    Chuck

    Example method below for others (does not have to be a webservice, method can sit in code behind):

            public partial class jqxGrid_ServerSide_FilteringPaging_Working : System.Web.UI.Page
    {
    [WebMethod]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public static object GetData(int pagenum, int pagesize, int testint, string teststring)
    {
    string result = "[{\"Surname\":\"Schumacher\",\"FirstNames\":\"Michael\"},{\"Surname\":\"Barrichello\",\"FirstNames\":\"Rubens\"},{\"Surname\":\"Alesi\",\"FirstNames\":\"Jean\"}]";
    return new JavaScriptSerializer().DeserializeObject(result);
    }
    }

    Thanks Peter – sorry I have already removed that line (I put a comment next to that line in the code above).

    Theres no difference with or without the record specified, so the issue lies elsewhere. Any further ideas?

    Cheers

    Thank you for the suggestion Peter.

    In reference to:
    http://www.jqwidgets.com/jquery-widgets-documentation/documentation/asp.net-integration/asp.net-grid-paging.htm

    I downloaded the example project files and successfully managed to run the code, calling the method and returning XML to the grid. It populates and runs perfectly.

    But, like my previous post, as soon as I have it return JSON to the grid, the grid does not display results. What do I need to change on your example page above (both markup and code behind) to have it successfully run using JSON?

    This is what I’ve tried (sorry if the formatting comes out bad):

    MARKUP

                source = {
    datatype: "json", // changed from "xml"
    datafields: [
    { name: 'CompanyName' },
    { name: 'ContactName' },
    { name: 'ContactTitle' },
    { name: 'City' },
    { name: 'Country' },
    { name: 'Address' }
    ],
    formatdata: function (data) {
    return { pagenum: data.pagenum, pagesize: data.pagesize }
    },
    record: 'Table', // removed this line
    url: 'Default.aspx/GetCustomers'
    };

    CODEBEHIND (now using a datatable instead of dataset)

            [WebMethod]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public static string GetCustomers(int pagenum, int pagesize)
    {
    string query = "SELECT * FROM ( "
    + " SELECT *, ROW_NUMBER() OVER (ORDER BY CustomerID) as row FROM Customers "
    + " ) a WHERE row > " + pagenum * pagesize + " and row <= " + (pagenum + 1) * pagesize;
    SqlCommand cmd = new SqlCommand(query);
    // Populate the DataSet.
    DataTable records = GetData(cmd);
    if (records != null)
    {
    // convert object to json string
    string result = GetJSONString(records);
    return result;
    }
    else
    {
    // return empty string
    return String.Empty;
    }
    }

    JSON STRING RETURNED
    [{“CustomerID”:”ALFKI”,”CompanyName”:”Alfreds Futterkiste”,”ContactName”:”Maria Anders”,”ContactTitle”:”Sales Representative”,”Address”:”Obere Str. 57″,”City”:”Berlin”,”Region”:””,”PostalCode”:”12209″,”Country”:”Germany”,”Phone”:”030-0074321″,”Fax”:”030-0076545″,”row”:”1″},{“CustomerID”:”ANATR”,”CompanyName”:”Ana Trujillo Emparedados y helados”,”ContactName”:”Ana Trujillo”,”ContactTitle”:”Owner”,”Address”:”Avda. de la Constitución 2222″,”City”:”México D.F.”,”Region”:””,”PostalCode”:”05021″,”Country”:”Mexico”,”Phone”:”(5) 555-4729″,”Fax”:”(5) 555-3745″,”row”:”2″}…]

    in reply to: Test Topic Test Topic #13197

    Sorry, I couldnt get code blocks to format correctly and retain indentation.

Viewing 4 posts - 1 through 4 (of 4 total)