1

enter image description here

Got this error while call the function

 static public void DisplayAJAXMessage(Control page, string msg)
{
    string myScript = String.Format("alert('{0}');", msg);

    ScriptManager.RegisterStartupScript(page, page.GetType(), "MyScript", myScript, true);
}

Calling this function:

    string sampledata = "Name                  :zzzzzzzzzzzzzzzz<br>Phone         :00000000000000<br>Country               :India";
        string sample = sampledata.Replace("<br>", "\n");


    MsgBox.DisplayAJAXMessage(this, sample);

I need to display Name,Phone and Country in next line.

4 Answers 4

9

Unterminated string constant means you've forgotten to close your string. You can't have an alert that runs over multiple lines. When the script is outputting to the browser, it's actually including the new lines.. not the "\n" like the javascript expects. That means, your alert call is going over multiple lines.. like this:

alert('Name                  :zzzzzzzzzzzzzzzz
       Phone         :00000000000000
       Country               :India');

..which won't work, and will produce the error you're seeing. Try using double backslash to escape the backslash:

string sample = sampledata.Replace("<br>", "\\n");
Sign up to request clarification or add additional context in comments.

3 Comments

How to set the font size as 14px and font as courier new font for that alert box.
You can't... Your best option is a custom alert dialog .. perhaps something with jQuery UI. That's a separate subject though..
how to create custom alert dialog box?
3

"\n" is a newline for C#, i.e. your js contains:

something('...blah foo
bar ...');

what you actually want is a newline in js:

something('...blah foo\nbar ...');

which you can do with:

string sample = sampledata.Replace("<br>", "\\n");

or:

string sample = sampledata.Replace("<br>", @"\n");

Comments

1

You need to escape/encode your string being consumed by JavaScript:

Escape Quote in C# for javascript consumption

Comments

0

Your Unterminated is not in C# is in Javascript generated code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.