Newline detected when add script in C# (ASP.NET)

  • Thread starter Thread starter Ivan Demkovitch
  • Start date Start date
I

Ivan Demkovitch

Hi!

I'm trying to add script to the page and place following code in my ascx
(control):


Page.RegisterClientScriptBlock("adder", "<script>function
doadd(){document.all.TextBox3.value=eval(document.all.TextBox1.value) +
eval(document.all.TextBox2.value)}</script>");

It doesn't work giving me all kind of problems...

I suspect the problem is with <> charachters.

How do I go around this problem?

Also, placing this line in code (I use WebMatrix) will mess up all HTML
code??

Thanks!
 
If that is the exact line from your code, then it just has to do with how
your formed your string. You cannot insert a line break in a string in C#.
It should work if you change your code to read:

Page.RegisterClientScriptBlock("adder", "<script>function " +
"doadd(){document.all.TextBox3.value=eval(document.all.TextBox1.value) +
" +
"eval(document.all.TextBox2.value)}</script>");

or, for more readability, I always for my javascript in a temp variable
first. Just kinda hard to read otherwise

string script = "<script>function " +
"doadd(){document.all.TextBox3.value=eval(document.all.TextBox1.value) +
" +
"eval(document.all.TextBox2.value)}</script>"
Page.RegisterClientScriptBlock("adder", script);

either way, both of those should work for you.

-Cliff
 
I solved problem...

Actually the problem was in "</script>"

Changed to "<" + "/" + "script>" and it works fine now.

Thanks!
 
Back
Top