简体   繁体   中英

Passing an array from javascript to code behind - C#

In my website, i'm declaring an array in javascript and insert them elements dynamically. So now, I want use that array from my C# code. I don't want use ajax to send that element to an web service... I just want use an C# event, like OnClick and access the array that was build in javascript.

I searched for an answer but I just found the oposite. Thanks

The easiest way is AJAX call and i don't understand why you are avoiding that ?

Make an AJAX call from your button click.

look here a demo : Ajax call is not calling to the server side and in httpfox shows error as "Error loading content (NS_ERROR_DOCUMENT_NOT_CACHED)" in ajax post call

for example : covert your array to a json string and call a web client in your c# code. Here i have a button . on button click i want to send my GRIDVIEW data to c# method(web method).

you need to remember that while sending json data using stringfy() method, in server side we need to define the parameter as object. not any other format like string/int/bla bla..... use Scripts/jquery-1.8.3.min.js http://code.jquery.com/ui/1.10.3/jquery-ui.js

 $('#btnResult').on('click', function () {

        var mydata = [];
        $("#<%=GridProjectDetails.ClientID %>  tr").each(function () {
            var myObject = new Object();
            var id = $(this).find("input[name*='ID']").val();
            var locationcode = $(this).find("input[name*='TextLocationCode']").val();
            var Location = $(this).find("input[name*='TextLocation']").val();
            myObject.id = id;
            myObject.locationcode = locationcode;
            myObject.Location = Location;
            mydata.push(myObject);
        });

        var myString = JSON.stringify({ details: JSON.stringify(mydata) });
        alert(myString);
        var exportdata = myString;

        $.ajax({
            type: "POST",
            url: "Default.aspx/ExportToExcel",
            data: exportdata,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                $("#Result").text(data.d);
            },
            error: function () { alert(arguments[2]); }
        });
    });
});

and server side method should be

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string ExportToExcel(object details)
{
     return "Message : Success";
}

It's a kinda weird thing to do, but if you have to do it, you can do it by creating a form, inside the form have a hidden text field and call a function when submitting to update the value of this field.

The markup:

<form id="yourForm" method="post" >
    <input type="text" name="hiddenFieldName" id="hiddenFieldName" hidden="hidden" />
</form>

The javascript:

void yourProcedure() {
    var yourArray = ["Value1", "Value2", "Value3", "Value4"];
    document.getElementById('hiddenFieldName').value = yourArray.join();
    document.getElementById("yourForm").submit();
}

Then in the server, the form variable will contain "Value1,Value2,Value3,Value4".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM