ASP.NET Webforms and DataTables

ASP.NET Webforms and DataTables

WilcoWilco Posts: 3Questions: 0Answers: 0
edited March 2011 in General
I'm trying to link data tables to an ASP.NET Page method (web service). Most of the literature I can find doing this says it should "just work" (but they use MVC). However I have run into a few issues.

The first (solved) was that DataTables didnt pass the Content-Type header needed so got the page HTML instead of the Web Service output. This necessitated a custom fnServerData function.

[code]

/* Initialise results Table */
$('#partSearchResults').dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": 'PartSearch.aspx/Test',
"fnServerData": function(sSource, aoData, fnCallback) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "PartSearch.aspx/Search",
data: aoData,
dataType: "json",
success: fnCallback
});
}
});
[/code]

However in this case aoData is not an object, its an array, with each item in the array being an object that hosts one name-value pair
[code]
[{"name":"sEcho","value":1},{"name":"iColumns","value":4},{"name":"sColumns","value":""},{"name":"iDisplayStart","value":0},{"name":"iDisplayLength","value":10},{"name":"sSearch","value":""},{"name":"bRegex","value":false},{"name":"sSearch_0","value":""},{"name":"bRegex_0","value":false},{"name":"bSearchable_0","value":true},{"name":"sSearch_1","value":""},{"name":"bRegex_1","value":false},{"name":"bSearchable_1","value":true},{"name":"sSearch_2","value":""},{"name":"bRegex_2","value":false},{"name":"bSearchable_2","value":true},{"name":"sSearch_3","value":""},{"name":"bRegex_3","value":false},{"name":"bSearchable_3","value":true},{"name":"iSortingCols","value":1},{"name":"iSortCol_0","value":0},{"name":"sSortDir_0","value":"asc"},{"name":"bSortable_0","value":true},{"name":"bSortable_1","value":true},{"name":"bSortable_2","value":true},{"name":"bSortable_3","value":true}]
[/code]

Which again from the literature I've read is wrong, and it should be a single object with those as properties.

Apologies if I've missed something obvious, My screen is going slightly blurry by now. Any pointers would be greatly appreciated.

Wilco

Replies

  • allanallan Posts: 63,175Questions: 1Answers: 10,409 Site admin
    It looks like whatever is doing the JSON serialisation on the server side is taking the name / value pair a little bit too far to me! It should be sEcho: 1, iColumns: 4 etc. I'm afraid I know nothing about ASP.NET Webforms though, so I can't offer any help other than to suggest looking that the documentation for whatever is creating the JSON and see if there is an option for making it do what is needed.

    Allan
  • WilcoWilco Posts: 3Questions: 0Answers: 0
    Thanks for the response. I worked out how to do it. ASP.NET Page methods require POSTed methods with the data in the HTTP body, as opposed to the standard method of using GET params. I wrote a conversion function to take the Name,Value array and make it a nice JS object that we can JSON, then modified Zack Owen's code to accept this on the server side.

    I wrote up my changes into a post: http://www.awilco.net/doku/datatables for anyone else who wants to use DataTables with ASP.NET Webform Page Methods.
This discussion has been closed.