问题
Is it possible to load and save data to/from a grid using c# code in the back from an aspx page, or does one have to use a web service (or PHP)? I have tried and failed using JSON.Net to map a very simple structure to code a backend structure
Is it possible to use JQuery (an ajax GET
I presume) to make a call to a method in the backend code file (.aspx.cs)? I have tried using code from various posts on this forum, but there is little information on the backend code (c#), and all seem to refer to web services. Any help/advice would be much appreciated.
Here is the JavaScript code associated:
var handsontable = $container.data('handsontable');
$(document).find('button[name=load]').click(function () {
$.ajax({
url: "Default.aspx/getJSData",
dataType: 'json',
type: 'GET',
//contentType: "application/json; charset=utf-8",
success: function (res) {
handsontable.loadData(res.data);
$console.text('Data loaded');
},
error: function () {
$console.text('Load error');
}
});
});
回答1:
You still need to do an Ajax call but you don't need to do a web service (you can). The function you want exposed to the Ajax call put the [WebMethod]
Attribute on and an use a Script Manager with the EnablePageMethods attribute set to true.
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
</asp:ScriptManager>
Method to Access:
[WebMethod]
public static void SomeFunction(string message, string name)
{
}
Ajax call using jQuery
(function($) {
$.ajax({
type: "POST",
url: "test.aspx/SomeFunction",
data: "{message:'Hello World', name: 'Bob'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert('success');
}
});
});
Reference: Using the WebMethod Attribute
来源:https://stackoverflow.com/questions/15681661/handsontable-grid-loading-and-saving-data-from-aspx-web-page