简体   繁体   中英

Unterminated string constant error in asp.net

在此处输入图片说明

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.

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");

"\\n" is a newline for C#, ie 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");

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

Escape Quote in C# for javascript consumption

您的Unterminated不在C#中,是在Javascript生成的代码中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM