0

In my aspx code I have something like this

if(AnId == <%=MyCodebehindObject.MyId%>)
{
    // stuff
}

The code is working: I can get the value from the codebehind variable. However, I'm getting a "Syntax error" warning right at the end of the first line.

I can "fixed it" by enclosing the variable with quotes, but this will make my variable a string, and I'm expecting an int. That will work just fine, the javascript will cast it for comparison, but I think that is just stupid and I was trying to find the write way of doing it.

Any ideas?

5
  • is if the if statement javascript? Commented Apr 15, 2013 at 17:23
  • yes, the if is a javascript statement Commented Apr 15, 2013 at 17:26
  • ok, thanks, you need to wrap the codebehind in quotes see my answer Commented Apr 15, 2013 at 17:27
  • MyCodebehindObject.MyID What is this..? any Control's ID..? Commented Apr 15, 2013 at 17:29
  • No. It's the page codebehind object, where the codebehind variable vives. Commented Apr 15, 2013 at 17:32

1 Answer 1

3

you need to wrap the javascript code behind in quotes

if(AnId == '<%= MyCodebehindObject.MyID%>'){
    // stuff
}
Sign up to request clarification or add additional context in comments.

3 Comments

I noticed that, I was just wondering if there wasn't a way of not doing that, since I see no point of making my variable a string to later on compare it with an int.
perhaps I am not understanding what you're asking. What is giving you the syntax error, The Javascript console or the .net?
The .net is, the visual studio in the "Error List" window.

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.