Server side(code behind) method does not call in asp.net
Server side(code behind) method does not call in asp.net
Hi all,
I'm new in jquery datatables, i m working to bind data from database in datatable with using this jquery datatable.
I m working on Asp.net webforms.
Problem is that code behind method(binddata) not getting call.
Please see my below code.
var oTable;
$(document).ready(function () {
oTable = $('#myDataTable').dataTable({
"bPaginate": true,
"bStateSave": true,
"sPaginationType": "full_numbers",
"bAutoWidth": true,
"bJQueryUI": false,
"bLengthChange": true,
"bServerSide": true,
"sAjaxSource": "Default.aspx/binddata",
"fnServerData": function (sUrl, aoData, fnCallback, oSettings) {
debugger;
//var jsonIn = '{ delimiter: "|", pageSize: 100, skip: ' + aoData[3].value + ', sortColumn: ' + aoData[2].value + ' }';
//+ ', sortDirection: "' + aoData[48].value + '", echo: ' + aoData[0].value + ' }'
//alert(jsonIn);
//var data = JSON.stringify(aoData);
$.ajax({
type: "POST",
url: sUrl,
data: aoData,
contentType: "application/json",
dataType: "json",
success: function (data) {
alert(data);
},
error: function (e) {
debugger;
alert(e);
}
});
},
"aoColumns": [
//Assign the data to rows
{"mDataProp": "ID", "bVisible": false },
{ "mDataProp": "COMPANY_NAME" },
{ "mDataProp": "ADDRESS" },
{ "mDataProp": "TOWN" }
]
});
});
Code behind method is
[WebMethod]
public static string binddata(DotnetExamplescls param)
{
var allCompanies = Company.GetCompanies();
IEnumerable filteredCompanies;
//Check whether the companies should be filtered by keyword
if (!string.IsNullOrEmpty(param.sSearch))
{
//Used if particulare columns are filtered
var nameFilter = Convert.ToString(HttpContext.Current.Request["sSearch_1"]);
var addressFilter = Convert.ToString(HttpContext.Current.Request["sSearch_2"]);
var townFilter = Convert.ToString(HttpContext.Current.Request["sSearch_3"]);
//Optionally check whether the columns are searchable at all
var isNameSearchable = Convert.ToBoolean(HttpContext.Current.Request["bSearchable_1"]);
var isAddressSearchable = Convert.ToBoolean(HttpContext.Current.Request["bSearchable_2"]);
var isTownSearchable = Convert.ToBoolean(HttpContext.Current.Request["bSearchable_3"]);
filteredCompanies = Company.GetCompanies()
.Where(c => isNameSearchable && c.Name.ToLower().Contains(param.sSearch.ToLower())
||
isAddressSearchable && c.Address.ToLower().Contains(param.sSearch.ToLower())
||
isTownSearchable && c.Town.ToLower().Contains(param.sSearch.ToLower()));
}
else
{
filteredCompanies = allCompanies;
}
var isNameSortable = Convert.ToBoolean(HttpContext.Current.Request["bSortable_1"]);
var isAddressSortable = Convert.ToBoolean(HttpContext.Current.Request["bSortable_2"]);
var isTownSortable = Convert.ToBoolean(HttpContext.Current.Request["bSortable_3"]);
var sortColumnIndex = Convert.ToInt32(HttpContext.Current.Request["iSortCol_0"]);
Func orderingFunction = (c => sortColumnIndex == 1 && isNameSortable ? c.Name :
sortColumnIndex == 2 && isAddressSortable ? c.Address :
sortColumnIndex == 3 && isTownSortable ? c.Town :
"");
var sortDirection = HttpContext.Current.Request["sSortDir_0"]; // asc or desc
if (sortDirection == "asc")
filteredCompanies = filteredCompanies.OrderBy(orderingFunction);
else
filteredCompanies = filteredCompanies.OrderByDescending(orderingFunction);
var displayedCompanies = filteredCompanies.Skip(param.iDisplayStart).Take(param.iDisplayLength);
var result = from c in displayedCompanies select new[] { Convert.ToString(c.ID), c.Name, c.Address, c.Town };
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string json = JsonConvert.SerializeObject(new
{
sEcho = param.sEcho,
iTotalRecords = allCompanies.Count(),
iTotalDisplayRecords = allCompanies.Count(),
aaData = allCompanies
});
return json;
}
but bind data method not call when run application.
I'm new in jquery datatables, i m working to bind data from database in datatable with using this jquery datatable.
I m working on Asp.net webforms.
Problem is that code behind method(binddata) not getting call.
Please see my below code.
var oTable;
$(document).ready(function () {
oTable = $('#myDataTable').dataTable({
"bPaginate": true,
"bStateSave": true,
"sPaginationType": "full_numbers",
"bAutoWidth": true,
"bJQueryUI": false,
"bLengthChange": true,
"bServerSide": true,
"sAjaxSource": "Default.aspx/binddata",
"fnServerData": function (sUrl, aoData, fnCallback, oSettings) {
debugger;
//var jsonIn = '{ delimiter: "|", pageSize: 100, skip: ' + aoData[3].value + ', sortColumn: ' + aoData[2].value + ' }';
//+ ', sortDirection: "' + aoData[48].value + '", echo: ' + aoData[0].value + ' }'
//alert(jsonIn);
//var data = JSON.stringify(aoData);
$.ajax({
type: "POST",
url: sUrl,
data: aoData,
contentType: "application/json",
dataType: "json",
success: function (data) {
alert(data);
},
error: function (e) {
debugger;
alert(e);
}
});
},
"aoColumns": [
//Assign the data to rows
{"mDataProp": "ID", "bVisible": false },
{ "mDataProp": "COMPANY_NAME" },
{ "mDataProp": "ADDRESS" },
{ "mDataProp": "TOWN" }
]
});
});
Code behind method is
[WebMethod]
public static string binddata(DotnetExamplescls param)
{
var allCompanies = Company.GetCompanies();
IEnumerable filteredCompanies;
//Check whether the companies should be filtered by keyword
if (!string.IsNullOrEmpty(param.sSearch))
{
//Used if particulare columns are filtered
var nameFilter = Convert.ToString(HttpContext.Current.Request["sSearch_1"]);
var addressFilter = Convert.ToString(HttpContext.Current.Request["sSearch_2"]);
var townFilter = Convert.ToString(HttpContext.Current.Request["sSearch_3"]);
//Optionally check whether the columns are searchable at all
var isNameSearchable = Convert.ToBoolean(HttpContext.Current.Request["bSearchable_1"]);
var isAddressSearchable = Convert.ToBoolean(HttpContext.Current.Request["bSearchable_2"]);
var isTownSearchable = Convert.ToBoolean(HttpContext.Current.Request["bSearchable_3"]);
filteredCompanies = Company.GetCompanies()
.Where(c => isNameSearchable && c.Name.ToLower().Contains(param.sSearch.ToLower())
||
isAddressSearchable && c.Address.ToLower().Contains(param.sSearch.ToLower())
||
isTownSearchable && c.Town.ToLower().Contains(param.sSearch.ToLower()));
}
else
{
filteredCompanies = allCompanies;
}
var isNameSortable = Convert.ToBoolean(HttpContext.Current.Request["bSortable_1"]);
var isAddressSortable = Convert.ToBoolean(HttpContext.Current.Request["bSortable_2"]);
var isTownSortable = Convert.ToBoolean(HttpContext.Current.Request["bSortable_3"]);
var sortColumnIndex = Convert.ToInt32(HttpContext.Current.Request["iSortCol_0"]);
Func orderingFunction = (c => sortColumnIndex == 1 && isNameSortable ? c.Name :
sortColumnIndex == 2 && isAddressSortable ? c.Address :
sortColumnIndex == 3 && isTownSortable ? c.Town :
"");
var sortDirection = HttpContext.Current.Request["sSortDir_0"]; // asc or desc
if (sortDirection == "asc")
filteredCompanies = filteredCompanies.OrderBy(orderingFunction);
else
filteredCompanies = filteredCompanies.OrderByDescending(orderingFunction);
var displayedCompanies = filteredCompanies.Skip(param.iDisplayStart).Take(param.iDisplayLength);
var result = from c in displayedCompanies select new[] { Convert.ToString(c.ID), c.Name, c.Address, c.Town };
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string json = JsonConvert.SerializeObject(new
{
sEcho = param.sEcho,
iTotalRecords = allCompanies.Count(),
iTotalDisplayRecords = allCompanies.Count(),
aaData = allCompanies
});
return json;
}
but bind data method not call when run application.
This discussion has been closed.