By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,670 Members | 1,431 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,670 IT Pros & Developers. It's quick & easy.

Using stringbuilder with javascript alert

P: n/a
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.GetTy pe(), 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?

*** Sent via Developersdex http://www.developersdex.com ***
Jul 11 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Mike P <mi*******@gmail.comwrote:
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.GetTy pe(), 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.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 11 '08 #2

P: n/a
In stringbuilder try to add new lines as as '//n'...

Leo

"Mike P" <mi*******@gmail.comwrote in message
news:eG**************@TK2MSFTNGP05.phx.gbl...
>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.GetTy pe(), 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?

*** Sent via Developersdex http://www.developersdex.com ***
Jul 11 '08 #3

P: n/a
Leo Seccia <ls*****@msn.comwrote:
In stringbuilder try to add new lines as as '//n'...
Careful with the slash direction - it should be "\\n" instead of "//n".

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 11 '08 #4

P: n/a
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.
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Leo Seccia <ls*****@msn.comwrote:
>In stringbuilder try to add new lines as as '//n'...

Careful with the slash direction - it should be "\\n" instead of "//n".

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 11 '08 #5

P: n/a
On Fri, 11 Jul 2008 02:55:57 -0700, Leo Seccia <ls*****@msn.comwrote:
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.
Well, maybe. It could just be a poorly-stated question. He does have a
variable named "sb" that he fails to show how it's initialized or used.
Maybe that's the StringBuilder instance to which he's referring. :)
Jul 11 '08 #6

P: n/a
In article <MP*********************@msnews.microsoft.com>, Jon Skeet
[C# MVP] <sk***@pobox.comwrote:
Mike P <mi*******@gmail.comwrote:
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.GetTy pe(), 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.
Or (if he had been using a string builder) sb.AppendLine("value")....

--
J.B. Moreno
Jul 12 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.