Using a javascript alert box

D

Dave

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
 
J

Jon Skeet [C# MVP]

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.
 
D

Dave

You nailed it. Almost embarassed to say how long I spent on this.

Thanks a lot, Jon.

Dave



[snip]
 
H

Hans Kesting

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
 
D

Dave

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
 

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