简体   繁体   English

将数组从JavaScript传递到背后的代码-C#

[英]Passing an array from javascript to code behind - C#

In my website, i'm declaring an array in javascript and insert them elements dynamically. 在我的网站上,我用javascript声明了一个数组,并动态地将它们插入元素。 So now, I want use that array from my C# code. 所以现在,我想从我的C#代码中使用该数组。 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. 我不想使用ajax将该元素发送到Web服务...我只想使用C#事件(例如OnClick)并访问在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 ? 最简单的方法是AJAX通话,我不明白您为什么要避免这种情况?

Make an AJAX call from your button click. 通过单击按钮进行AJAX呼叫。

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 看这里的演示: Ajax调用未调用服务器端,并且在httpfox中显示错误为ajax发布调用中的“错误加载内容(NS_ERROR_DOCUMENT_NOT_CACHED)”

for example : covert your array to a json string and call a web client in your c# code. 例如:将数组隐式转换为json字符串,然后使用c#代码调用Web客户端。 Here i have a button . 这里我有一个按钮。 on button click i want to send my GRIDVIEW data to c# method(web method). 在按钮上单击我想将我的GRIDVIEW数据发送到c#方法(网络方法)。

you need to remember that while sending json data using stringfy() method, in server side we need to define the parameter as object. 您需要记住,在使用stringfy()方法发送json数据时,在服务器端我们需要将参数定义为对象。 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 没有其他格式,例如string / int / bla bla .....使用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: 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". 然后在服务器中,表单变量将包含“ Value1,Value2,Value3,Value4”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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