Using The Result From Jquery Into C#
I have this function in jquery which has the result array and how can I get this result array to C# code. Can anyone help me regarding this. function generateData() { var result
Solution 1:
You could do this asynchronously through a web method call from script, such that you define a web method appropriately, then call and handle the data and potential return value, as desired. For example:
Defining a web method:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
publicstaticstringHandleData(object[] data)
{
//handle datareturnstring.Empty;
}
Defining a reusable jQuery script method to handle web method calls:
functionExecutePageMethod(page, fn, paramArray, successFn, errorFn) {
var paramList = '';
if (paramArray.length > 0) {
for (var i = 0; i < paramArray.length; i += 2) {
if (paramList.length > 0) paramList += ',';
paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';
}
}
paramList = '{' + paramList + '}';
$.ajax({
type: "POST",
url: page + "/" + fn,
contentType: "application/json; charset=utf-8",
data: paramList,
dataType: "json",
success: successFn,
error: errorFn
});
}
And, of course, the call itself:
ExecutePageMethod("Default.aspx", "HandleData",
["data", result], successCallback, failureCallback);
Naturally we now need to make sure our callback methods exist:
functionsuccessCallback(result) {
var parsedResult = jQuery.parseJSON(result.d);
}
functionfailureCallback(result) {
}
Solution 2:
Use a hiddenfield to store the result..
<asp:HiddenField id="hfResult" runat="server" />
JQuery
$('hfResult').val(result);
C#
Stringresult= hfResult.Value;
Note that a hiddenField only holds a string, so you might need to use some kind of seperator to seperate your array objects..
Post a Comment for "Using The Result From Jquery Into C#"