0

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!

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

|Thanks... that is what i was thinking
0

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, i.e.

<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());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.