I have a loop in JS, and I want to pass each parameter in that loop to a function in ASP.NET in the codebehind.
something like that:
for (var i = 0; i < elements.length; i++)
{
}
And I want to pass a function in <%%> the elements[i].
How can I do that?
thanks!
It's not that easy, the only way is to make a post and send the variables.
What I normally do is to have a handler to do the job for me and using jQuery I get a nice pretty effect...
from your javascript code
var r = '';
for (var i = 0; i < elements.length; i++)
r += elements[i] + ',';
// send this asynchronously to the handler
$.get("myHandler.asmx", { values: r }, function(data) {
// it finished processing, let use the passed data
alert(data);
});
in your handler
public void ProcessRequest(HttpContext context)
{
string r = context.Request["values"];
// process them
context.Response.ContentType = "text/plain";
context.Response.Write("OK");
}
that "OK" will be passed to the data
variable, and you can interact your user with a much more rich environment.
If you mean you would like to call a server side method from the client then there is an easier way, using Page-based web services:
In your code behind add a method as follows:
[WebMethod]
public static void MyMethod (string s)
{
}
It is necessary to use EnablePageMethods=true in the page's script manager, ie
<asp:ScriptManager runat="server" ID="ScriptManager1" EnablePageMethods="true">
Then in your client code you can use:
PageMethods.MyMethod (s, MyMethodSuccess, MyMethodFail);
function MyMethodSuccess() { }
function MyMethodFail(error) {
alert(error.get_message());
}
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.