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!

4
  • 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
    Commented Jun 5, 2013 at 15:12
  • What are you trying to accomplish? There's probably a much better way achieve it.
    – Mansfield
    Commented Jun 5, 2013 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
    Commented Jun 5, 2013 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
    Commented Jun 5, 2013 at 16:09

1 Answer 1

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...)

1
  • 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
    Commented Jun 5, 2013 at 16:14

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.