Calling Page Method with Parameters

Calling Page Method with Parameters

JosephPGiuntaJosephPGiunta Posts: 4Questions: 0Answers: 0
edited March 2012 in General
All,

Wondering if someone can help. Somewhat new to jQuery and very new to the DataTables plug in. In short:

I want to put a grid on a page loaded with data. The Page method expects 3 parameters and returns a list of objects (InvoiceRecords) . What I have below successfully calls the Page Method, but when the Page Methods returns I get a JS error. Specifically an error in the _fnAjaxUpdateDraw function.

Here is the exact line --
[code]
var aData = _fnGetObjectDataFn(oSettings.sAjaxDataProp)(json); //<-- json has the server data; aData is undefined!
for (var i = 0, iLen = aData.length; i < iLen; i++) {
[/code]

And here is my code:
Page Method
[code]
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static List GetInvoiceRecords(string DataSource, DateTime StartDate, DateTime EndDate) {
List invoiceRecs = HwcInvoicesDAL.GetInvoiceRecords(DataSource, StartDate, EndDate);
return invoiceRecs;
}
[/code]

JS
[code]
$('#example').dataTable({
'bProcessing': true
, 'bServerSide': true
, 'sAjaxSource': 'HwcCreateInvoice.aspx/GetInvoiceRecords'
, 'fnServerData': function (sSource, aoData, fnCallback) {
$.ajax({
'dataType': 'json'
, 'contentType': 'application/json;'
, 'type': 'POST'
, 'url': sSource
, 'data': "{DataSource: 'demo', StartDate: '01/01/2010', EndDate: '12/31/2010'}"
, 'success': fnCallback
});
}
});
[/code]

Replies

  • JosephPGiuntaJosephPGiunta Posts: 4Questions: 0Answers: 0
    Really hoping someone can help me here. I've added the 'sAjaxDataProp' to the Ajax call and set it to 'd', since that is what the Asp.Net Page Method seems to be putting in the returned JSON string. What is being returned is an array of objects (InvoiceRecords) from the page method.

    Now the PageMethod has it's response format set to JSON, so what the client should be getting is JSON. When I inspect, it is an array of objects that is on the client side.

    Is this acceptable to have the dataGrid load a list of objects? Do I have to somehow change the js file From:

    [code]
    var aData = _fnGetObjectDataFn(oSettings.sAjaxDataProp)(json);
    [/code]

    To:

    [code]
    var aData = _fnGetObjectDataFn(oSettings.sAjaxDataProp)(JSON.stringify(json));
    [/code]

    I would love to use this plug in instead of my little home grown version, but I cannot seem to get it to load data returned from a Page Method.
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    > Is this acceptable to have the dataGrid load a list of objects?

    yes absolutely - use mDataProp: http://datatables.net/blog/Extended_data_source_options_with_DataTables

    What does your JSON data look like?

    Allan
This discussion has been closed.