-2

What is wrong in the below code?

ClientScript.RegisterStartupScript(
    Me.GetType(),
    "key",
    "return confirm('Are you sure you wish to delete these records?');",
    True)
6
  • What do you want it to do? Can you add a bit more context? Commented Feb 14, 2012 at 7:05
  • Need more code. Upload a working/buggy code to JSFiddle. Commented Feb 14, 2012 at 7:06
  • 1
    What makes you think something is wrong with it? Your question title suggests you are receiving an error message. What is it? Is it a C# error or a JavaScript error? If the latter, why are you showing us C# and not the HTML/JS it outputs? Commented Feb 14, 2012 at 7:06
  • This question should not be closed. While it is lacking in detail, there is enough information to identify a definite problem. I would suggest to the OP that they put more effort into their questions in the future. Commented Feb 14, 2012 at 7:40
  • Similar: JavaScript error “Return statement outside of function” Commented Oct 8, 2020 at 20:22

2 Answers 2

1

The JavaScript error "return statement outside of function" means you've created a code fragment that is not allowed to exist outside of a function definition.

Sign up to request clarification or add additional context in comments.

Comments

1

You are registering a startup script; a startup script is intended to be run (no surprise) on startup. By itself, your code cannot be run on startup--there is nothing logical for it to do, i.e. where would it return?

However, JavaScript code outside of a function is legal in many cases. The following code would work fine.

Example (VB.NET)

ClientScript.RegisterStartupScript(
    Me.GetType(),
    "key",
    "alert('I am a client-side script!');",
    True)

Your particular code either needs to be put into a function and called when desired, or attached to a client-side event.

Since the intention is a delete prompt, here is an example attached to btnDelete.

Example (C#)

this.btnDelete.Attributes["onclick"] = "return confirm('Are you sure you wish to delete these records?');";

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.