Using stringbuilder with javascript alert

M

Mike P

I am trying to build up some text in a stringbuilder and then use
javascript to show it with an alert box.

Here is my attempt to show it in an alert box :
string str = "";
str += "<script language='javascript'>";
str += "alert('" + sb + "')";
str += "</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), key,
str);

This works fine until I start trying to add /n's to my stringbuilder to
force new lines. When I do this I get the error 'unterminated string
constant'.

Can anybody help?
 
J

Jon Skeet [C# MVP]

Mike P said:
I am trying to build up some text in a stringbuilder and then use
javascript to show it with an alert box.

Here is my attempt to show it in an alert box :
string str = "";
str += "<script language='javascript'>";
str += "alert('" + sb + "')";
str += "</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), key,
str);

This works fine until I start trying to add /n's to my stringbuilder to
force new lines. When I do this I get the error 'unterminated string
constant'.

Can anybody help?

Well for a start, that's not using StringBuilder at all.

To get a string with the contents "\n" you need to escape the
backslash, so you end up with "\\n" in your source code.
 
L

Leo Seccia

lol, the initial description of the problem tricked me (it said /n)... and
as you sat that isn't stringbuilder at all, he is just concatenating a
string.
O well, good old Friday.
 
J

J.B. Moreno

Jon Skeet said:
Well for a start, that's not using StringBuilder at all.

To get a string with the contents "\n" you need to escape the
backslash, so you end up with "\\n" in your source code.

Or (if he had been using a string builder) sb.AppendLine("value")....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top