开发者

using the result from jQuery into C#

开发者 https://www.devze.com 2023-04-12 17:39 出处:网络
I have this function i开发者_开发知识库n jquery which has the result array and how can I get this result array to C# code. Can anyone help me regarding this.

I have this function i开发者_开发知识库n 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 = $('#accordion').sortable('toArray');
 }


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)]
public static string HandleData(object[] data)
{
    //handle data
    return string.Empty;
}

Defining a reusable jQuery script method to handle web method calls:

function ExecutePageMethod(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:

function successCallback(result) {
    var parsedResult = jQuery.parseJSON(result.d);
}

function failureCallback(result) {

}


Use a hiddenfield to store the result..

<asp:HiddenField id="hfResult" runat="server" />

JQuery

$('hfResult').val(result);

C#

String result = 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..

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号