1

I am using charts.js in one of the aspx page. Everything works except for one issue. Consider the code below,

window.myBar = new Chart(context).Bar(chData, {
    showScale: true,
    scaleLabel : "<%= Number(value).toFixed(0).replace('.', ',') + ' %'%>",
});

This works fine in a plain html page, but when used on the aspx page, the compiler tries to evaluate the code as aspx due to the <%= syntax that is used in aspx and it creates problems. Is there a way I can use this javascript code in aspx?

1
  • You mean that <%= should come in the actual HTML document? Commented Jul 14, 2015 at 9:19

4 Answers 4

1

You can try by escaping the characters (symbolsw) and see if aspx still recognizes them (the expected is it should recognize them as plain text now).

"\<\%\= Number(value).toFixed(0).replace('.', ',') + ' %'\%\>"

or alternatively you can place your JavaScript code in a separate file

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

2 Comments

Escaping like this "<\%= works, I just figured that out after posting the question.
Hi, since lack of reputation to comment the other answer, for better/worse, its upto you but if its for readability i'd prefer using escape character and also it seems majority uses escape character for simplicity
1

It feels like a workaround, but it does the job:

"<" + "%=

I do wonder though why one would want this.

2 Comments

It works. Also, I just figured out "<\%= works too. Would that be any better/worse?
I am not sure, haven't programmed WebForms for years now.
1

You can also make it a string:

<%= @"

window.myBar = new Chart(context).Bar(chData, {
        showScale: true,
        scaleLabel : ""<%= Number(value).toFixed(0).replace('.', ',') + ' %'%>"",
        });
" %>

Comments

1

replace "" in "<%= Number(value).toFixed(0).replace('.', ',') + ' %'%>" with '' and ' %' to " %" and make it '<%= Number(value).toFixed(0).replace('.', ',') + " %"%>'. im new to this but this seemed to work for me

Comments

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.