最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Unterminated string constant error in asp.net - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question asked Aug 13, 2012 at 6:58 Prince Antony GPrince Antony G 9324 gold badges18 silver badges39 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 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");

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

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

Escape Quote in C# for javascript consumption

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

发布评论

评论列表(0)

  1. 暂无评论