Haroro
Haroro

Reputation: 21

Variable in the JavaScript in C#

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?

Upvotes: 2

Views: 60

Answers (1)

David
David

Reputation: 219096

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

Upvotes: 3

Related Questions