2

How can I call a .net method in inline code using a javascript variable? My code looks like this:

for (var i = 0; i < numberIterations; i++) 
{ 
    var result = <%# GetFilterTagURL( myArray[i].Value, false) %>;
    //do stuff with result
}

This block is inside a javascript block; the GetFilterTagURL method is a a .net method and I want to pass the variable myArray[i].Value as the first parameter.

Any ideas?

Update: All your answers are helpful. I guess that, as you say, I'll have to create a web service to achieve what I want.

1
  • What exactly you want to achieve? If you want to loop N times in the code behind you will need to post this value to some handler, page or webservice and get the response value. Commented Mar 28, 2011 at 16:39

3 Answers 3

1

You will not be able to accomplish it like that, since the <%# %> tag is used for Data binding.

One approach however would be to create a webservice and call it from your javascript. Inside your webservice you can then make the call to GetFilterTagURLand use the result in your javascript.

Check out this article from MSDN for more info.

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

Comments

1

You cannot send client-script values to the server-side. The oposite is valid though, you can register a javascript block from your code-behind.

See Page.RegisterStartupScript method.

Comments

0

this will work, but you have to call Page.DataBind() in your code

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.