469,271 Members | 997 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Using a javascript alert box

I am beginning to write some C# pages to an app I created with VB.Net.
There is one VB routine I use frequently - a javascript alert box. I have
created a C# version, but it does not handle newlines. I get an
"unterminated string constant" error message. Googling this problem gives
some hints that there are others with this problem, but I could not find a
solution.

In addition to this method, I tried the RegisterClientScriptBlock way, but
get the same results. I have also tried several ways of building the string
(enclosing with single quotes & double quotes, the ToString() method, etc.)
without success. I should add that I can use the C# routine without
incident if I pass a normal string variable. It's only when I pass in a
newline character (and I suspect other escaped characters) that the problem
occurs.

I am enclosing some code below. If somebody could help me out with this
problem, I would greatly appreciate it.

First VB.Net:

Public Sub DisplayAlert(ByVal msg As String)
response.write("<script language=JavaScript> alert('" & msg & "'); <" &
"/script>")
end sub

Calling this routine the following way gives the expected result:

dim s as string = "Line 1\n\rLine 2"
DisplayAlert( s )
Result:

Line 1
Line 2

----------------------------------------------------------------
----------------------------------------------------------------

Now in C#:

private void DisplayAlert(string msg)
{
Response.Write("<script language=JavaScript> alert('" + msg + "'); <" +
"/script>");
}

Calling this routine the following way gives the 'unterminated string
constant' error.

string s = "Line 1\nLine 2";
DisplayAlert( s );
Can anyone help me?

Thanks,

Dave
Feb 14 '06 #1
4 22439
Dave <da*****************************@stic.net> wrote:

<snip>
Now in C#:

private void DisplayAlert(string msg)
{
Response.Write("<script language=JavaScript> alert('" + msg + "'); <" +
"/script>");
}

Calling this routine the following way gives the 'unterminated string
constant' error.

string s = "Line 1\nLine 2";
DisplayAlert( s );
Can anyone help me?


The problem is that you want the string to be literally
"Line 1\nLine2" whereas the code you've written makes it actually:

"Line 1
Line 2"

(i.e. with a newline in it). You'll need to escape the backslash:

string s = "Line 1\\nLine 2";

That should do the trick.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 14 '06 #2
You nailed it. Almost embarassed to say how long I spent on this.

Thanks a lot, Jon.

Dave

[snip]
(i.e. with a newline in it). You'll need to escape the backslash:

string s = "Line 1\\nLine 2";

That should do the trick.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Feb 14 '06 #3
>
private void DisplayAlert(string msg)
{
Response.Write("<script language=JavaScript> alert('" + msg + "'); <" +
"/script>");
}


Note: you don't need to "break the </script> tag" as in old ASP.
Codebehind sees nothing special in that string. In fact, I suspect the
compiler just joins the two (constant) parts.

Hans Kesting
Feb 15 '06 #4
Thank you for that insight, Hans. I develop in an environment where all the
apps are inline, not code-behind, and don't have any say in that issue. If
I change it to this -

Response.Write("<script language=JavaScript> alert('" + msg + "');
</script>");

I get the old 'CS1010: Newline in constant' error.

Nevertheless, I'm glad you pointed that out. Breaking up that tag is
inelegant at best, and I'm sure I'll put your pointer to use some time in
the future.

Dave
"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
news:mn***********************@spamgourmet.com...

private void DisplayAlert(string msg)
{
Response.Write("<script language=JavaScript> alert('" + msg + "'); <" +
"/script>");
}


Note: you don't need to "break the </script> tag" as in old ASP.
Codebehind sees nothing special in that string. In fact, I suspect the
compiler just joins the two (constant) parts.

Hans Kesting

Feb 15 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Tasha's Dad | last post: by
2 posts views Thread by Kenneth | last post: by
6 posts views Thread by bonehead | last post: by
2 posts views Thread by Kenneth | last post: by
5 posts views Thread by Dave | last post: by
2 posts views Thread by Seth Williams | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.