ASP.NET Webforms and DataTables
ASP.NET Webforms and DataTables
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
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
This discussion has been closed.
Replies
Allan
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.