1

This is a simple syntax question, but I haven't been able to find (or haven't known quite how to phrase the question in order to find) an answer.

I'm trying to pass a Javascript variable into a .NET script like so:

var name = '<%=GetName(' + String(pageid) + ') %>';

GetName is a server-side .NET function, obviously. I get an "Expression Expected" server error thrown when I attempt to use it in its present state.

What's the problem with my syntax? Thanks!

Ryan
  • 127
  • 1
  • 10
  • 1
    During page composition, your scriptlet tags are evaluated, and after that process is complete, the javascript is then evaluated. You cannot get a javascript variable to resolve because its considered just text at that point in the process. – techsaint Jun 05 '13 at 15:12
  • What are you trying to accomplish? There's probably a much better way achieve it. – Mansfield Jun 05 '13 at 15:19
  • I've got a loop set up that's using the index as the argument for the function (pageid). The returned text is added to a div that's appended to the body and display in a fixed position for a few seconds. I'm sure there's a better way; I just don't know of one at the moment. – Ryan Jun 05 '13 at 15:25
  • I think I'm just going to use a few switch statements in my script and hard code the values. It won't be as maintainable this way, but there are only 8 different values, and they'll only be changing at most once per year. Thanks for the help – Ryan Jun 05 '13 at 16:09

1 Answers1

3

You cannot do that. By the time that JavaScript is executed, the .NET page has already been rendered and left the server. You have to figure out another way to do that (precompute the value, xhr...)

namero999
  • 2,882
  • 18
  • 24
  • So at the time the server renders, the JavaScript value is not yet available. That makes sense; a pretty basic fundamental that I obviously had glossed over at some point. – Ryan Jun 05 '13 at 16:14