2
ClientScript.RegisterStartupScript(
    Page.GetType(),
    "INFORMATION",
    @"PopupMessage(""INFORMATION"", ""Data was created successfully"", false);",
    true);

The code above show the JavaScript code in C# code behind

What I want to do is add the variable to the message

but I used this code which does not work:

ClientScript.RegisterStartupScript(
    Page.GetType(),
    "INFORMATION",
    @"PopupMessage(""INFORMATION"", """+Data+" was created successfully"", false);",
    true);

It throws error:

Error CS1501 No overload for method 'RegisterStartupScript' takes 5 arguments

Does someone know how to make it work?

4
  • The Data is a String type data after add "+Data+" the code fail to run . It show CS1003: Syntax Error, ',' expected Commented Aug 16, 2021 at 14:43
  • Severity Code Description Project File Line Suppression State Error CS1501 No overload for method 'RegisterStartupScript' takes 5 arguments Severity Code Description Project File Line Suppression State Error CS1003 Syntax error, ',' expected Commented Aug 16, 2021 at 14:45
  • That @ only applies to the string literal immediately following it, not to any other literals in the same expression - see David's answer Commented Aug 16, 2021 at 14:51
  • Ya haha It's work new to C# Thank you Hans Kesting too~~ Commented Aug 16, 2021 at 14:52

1 Answer 1

3

This string literal is a syntax error:

" was created successfully"", false);"

One of the things the verbatim identifier does for a string is change the way double-quotes are interpreted. Without it, double-quotes need to be escaped. But with it, escaping doesn't work (because it's a verbatim string) so a special consideration needs to be made for double-quotes. To add them to a string you need to "double" them.

This is why your other string literal works as expected:

@"PopupMessage(""INFORMATION"", """

To achieve the same thing in the failing string literal, use the verbatim identifier again:

@" was created successfully"", false);"
Sign up to request clarification or add additional context in comments.

2 Comments

OMG! It's Works!! Thank You David ~~ Thank you so much~
You can mark it as answer if it works for you

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.