简体   繁体   中英

Retrieve values from jQuery to C# Code

I have a jQuery script like this:

var sList = "";
$('input[type=checkbox]').each(function () {
var sThisVal = (this.checked ? "1" : "0");
sList += (sList=="" ? sThisVal : "," + sThisVal);
});
console.log (sList);

But this script just writes sList values to console. How can I retrieve sList values in C# code? and use it.

Alternatively, if you don't want to do a full postback, you could consider using the ASP.NET AJAX Server Callback mechanism.

This will allow you to send your sList to a server-side web service, do whatever server-side processing you want, and then return the JSON-encoded result to the client.

Add a hidden field:

<asp:HiddenField runat="server" ID="myList"/>

and write a JavaScript:

$("input[type=hidden][id$=myList]").val(sList);

and now you should get your sList on your server events , just see myList.Value from your C# code

you can simply add a hidden field to your asp.net page and set its value as a comma separated string using your JS function.

form the C# just get the data saved in the hidden field..

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