0

In my .aspx I've got the following javascript variable defined:

var action = <%=ProdView %>

In code-behind this returns a custom enum value:

protected ProductView ProdView { get; private set; }

I would figure that this would automatically be converted to a string in javascript? Looks like no because I get the runtime error "Item is not defined" where Item is the value ProdView.Item. Ultimately I want the action's value to be "Item" as the value.

Here's the Enum:

  public enum ProductView
  {
   Product,
   Item
  }

1 Answer 1

5
var action = '<%=ProdView.ToString() %>'

Don't forget the quotes.

Edit to respond on coffee addicts comment

You have to remember that the code is executed twice, first at server side to generate the text string:

var action = '<%=ProdView.ToString() %>'

is executed by ASP.net and turned into a complete string before returning it to the web browser

var action = 'lalalalala'

And the actual java script is executed in the web browser.

So ASP.net have nothing to do with the actual javascript execution. It's job is only to generate HTML/javascript/css that will be sent back to the webbrowser.

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

2 Comments

Yep - what he said. Even mine didn't have the quotes - and mine would break too.
I guess I don't understand under the hood how JS interprets this line of code in terms of shoving server-side references into a string.

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.