albert
albert

Reputation: 33

Use javascript variable into inlined code

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.

Upvotes: 2

Views: 947

Answers (3)

BrunoLM
BrunoLM

Reputation: 100361

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.

Upvotes: 1

Robert
Robert

Reputation: 3302

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

Upvotes: 0

jmccarthy
jmccarthy

Reputation: 480

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.

Upvotes: 1

Related Questions