简体   繁体   English

将变量从jquery传递到后面的代码

[英]Pass variable from jquery to code behind

<asp:Button ID="Button1" runat="server" Text="Submit" />
     <script>
        $(document).ready(function () {
           $("#Button1").click(function () {
                var list = [];
                var a= $("#TextBox1").val();
                var b= $("#TextBox2").val();
                var count = a* b;
                list.push('test');
                for (var i = 1; i <= count; i++) {
                   var item = $("#DropDownList_" + i + "_").find(":selected").text();
                   list.splice(i, 0, item);
                   console.log(list.join());
                   alert(list[i]);
                 }              
             });
       });
     </script>

Hello guys first time at the stack! 大家好,第一次来栈! I have this jquery code that get all choices of dropdown list and store it in array. 我有此jquery代码,可获取下拉列表的所有选择并将其存储在数组中。 I want pass this array to c# so I can store the values in DB, please help. 我想将此数组传递给c#,以便将值存储在DB中,请帮忙。

You can simply do this using jQuery ajax:- 您可以简单地使用jQuery ajax做到这一点:

var dataToSend = { "Data": list};
$.ajax({
           type: "POST",
           dataType: "json",
           contentType: "application/json; charset=utf-8",
           url: "Default.aspx/SaveData",
           data: JSON.stringify(dataToSend),
           success: function (msg) {
                //success data if any 
           },
           error: function (msg) { //handle error here }
     });

Add this method in your page behind (Default.aspx.cs):- 在(Default.aspx.cs)后面的页面中添加此方法:-

[WebMethod]
public static void SaveData(string[] Data)
{
     //Save data to DB
}

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

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