javascript asp.net 中未终止的字符串常量错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11929490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:47:42  来源:igfitidea点击:

Unterminated string constant error in asp.net

c#javascriptasp.net

提问by Prince Antony G

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.

我需要在下一行显示姓名、电话和国家。

回答by Simon Whitehead

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:

未终止的字符串常量意味着您忘记关闭字符串。您不能有一个跨越多行的警报。当脚本输出到浏览器时,它实际上包含了新行……而不是 javascript 期望的“\n”。这意味着,您的警报呼叫会跨越多行……像这样:

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

回答by Marc Gravell

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

"\n"是 C# 的换行符,即您的 js 包含:

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

what you actuallywant is a newline in js:

真正想要的是 js 中的换行符:

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

which you can do with:

你可以这样做:

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

or:

或者:

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

回答by rick schott

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

您需要对您的字符串进行转义/编码JavaScript

Escape Quote in C# for javascript consumption

C# 中的转义引号用于 javascript 消费

回答by Roger Medeiros

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

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