Pass javascript array to asp array
I have 开发者_如何学Pythona javascript array with values of checkbox selected. I want to pass this javascript array to asp array so that I can update the database.
I am using Jquery to get checkbox value
$('.checkboxes_to_delete:checked').each(function (key, value)
{
dataToBeDeleted.push($(this).val());
alert(dataToBeDeleted);
});
The easiest way is probably to use your Javascript to put the values into a hidden textbox on your page, and then your ASP code can pull that value down like any other form value.
Send your array using $.post:
//Javascript
var myarray = [1, 2, 3, 4, 5];
$.post("Default.aspx", { data: myarray }, function (r) { /* callback */ }, "text");
In the code-behind (Default.aspx.cs) you can access your array by using Request["data[]"]. Your data will be a CSV.
//C#
string v = Request["data[]"];
//v == "1,2,3,4,5"; (true)
What I have done in the past is put the data from the JavaScript array into a hidden textbox with commas between the values. Then post-back and read the hidden textbox, and use string.Split() to seperate out into an array server-side.
Have you considered a checkboxlist control?
精彩评论