473,719 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
"unterminat ed 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 RegisterClientS criptBlock 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(By Val msg As String)
response.write( "<script language=JavaSc ript> 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(st ring msg)
{
Response.Write( "<script language=JavaSc ript> 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 22587
Dave <da************ *************** **@stic.net> wrote:

<snip>
Now in C#:

private void DisplayAlert(st ring msg)
{
Response.Write( "<script language=JavaSc ript> 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.co m>
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.co m>
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(st ring msg)
{
Response.Write( "<script language=JavaSc ript> 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=JavaSc ript> 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.co m...

private void DisplayAlert(st ring msg)
{
Response.Write( "<script language=JavaSc ript> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2821
by: NewbieJon | last post by:
I am attempting to send the variable "sComputerName" from my ActiveX script to "GetInfo.asp" using javascript. (Having been advised this is the way to get my ActiveX variable into my ASP script) My code is below. I am getting the following error "MyForm.oNetwork.Value is Null or is not an object". I'm struggling to diagnose what to do next. I'd very much appreciate some help Many thanks <HTML><BODY><FORM NAME="MyForm"...
9
4526
by: CW | last post by:
I wrote an HTML based chat application. The front end is built entirely on HTML + javascript. Essentially, I have a hidden frame that's refreshed frequently and any new messages are displayed in another frame using document.write. My problem is that since chat screen can be obscured by other applications/windows, I have no way of informing users that new messages have arrived other than popping the window to the top using window.focus....
7
11143
by: Tasha's Dad | last post by:
A description of the problem: 1) Go to a page with various settings and a timeout (forces re-login if over 10 minutes) 2) Before the timeout, make some changes to settings. 3) Press a "reset to defaults" button that uses a confirmation box to let the user know what is about to be reset (lists items). 4) Walk away while the confirm box is displayed and come back after the page timeout. 5) My screen now has the page, a confirm popup on top...
2
2750
by: Kenneth | last post by:
I am tryint to view a xml document in Internet Exploer as a ActiveX object using Javascript but it does not work. I only see the button but nothing happens when i click it. I am trying to view the 'firstChild' of the xml document. here is the code; ---------------------------------------------------------------- <html>
6
3138
by: bonehead | last post by:
Greetings, I'm working on an e-mail form (btw many thanks to Philip Ronan for the very cool email address format tester function, best I've seen so far). I've been trying, with limited success, to produce a javascript alert box which shows the user a validation error message. The error message itself is dynamic, i.e., it's contained within a php variable. I seem to be running into two issues:
4
29917
by: PH | last post by:
Does anyone know how to have the pop-up javascript alert, Response.Write("<script language=javascript>alert('HEY!');</script>); , pop-up after the page has been posted back? I am pressing a button, validating data and then wanting to simply pop-up an alert if say the login is empty. When I click the button though the page is blank but the pop-up is there.
2
2881
by: Kenneth | last post by:
Hi, In a document I have three lines that detects and redirects to another page is a session variable is 0 If Session("Customer_ID")=0 Then Response.Redirect("myPage.aspx") End If What I really would like to have is a notification for the
5
1630
by: Dave | last post by:
I have an app with several web forms. Code for each of the forms contains a subroutine I wrote that performs the Javascript function "alert". Thus: Private Sub DisplayAlert(ByVal msg As String) Dim sKey As String = "s" Dim alertScript As String = "<script language=JavaScript runat=server> " alertScript &= "alert('" & msg & "');" alertScript &= "</script>" RegisterClientScriptBlock(sKey, alertScript) End Sub
3
3958
by: Wayne Deleersnyder | last post by:
Hi All, I'm trying to create a function that will cause a pop-up alert to appear if dates which were chosen from a drop-down list were invalid on a page. There's 4 dates, so there's the possibility of 4 errors at once. I'd like to pop-up all the errors at once, so the user can correct all the errors at once. My issue is format. I'd like a message something like... ERROR: Acquired Date Invalid - only 28 days in February for year
2
1973
by: Seth Williams | last post by:
In the middle of my event handler (for a button), I have an if/then statement (using vb.net): if (whatever) then strScript="<script ....... strScript &= alert('This is an alert') strscript &="</script> ' this is all an abbreviated script - - but you get the idea response.write(strscript) end if response.redirect("another page.aspx")
0
8828
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9360
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9208
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9123
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9053
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5971
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3185
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2562
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.