简体   繁体   中英

Raising Javascript confirm box from code behind

I am trying to raise a Javascript confirm box from code behind.

I am using the following syntax

ScriptManager.RegisterStartupScript(this, GetType(), "key", "javascript:return confirm('Are you sure you want to delete?');", true);

It is throwing an error:

return statement out of function

Please help.

Remove the return , you are not returning this value anywhere. The RegisterStartupScript is executing the function in the global context:

ScriptManager.RegisterStartupScript(
    this, 
    GetType(), 
    "key", 
    "confirm('Are you sure you want to delete?');", 
    true
); 

But this is probably something you should not even be doing on the server. You should ask for confirmation BEFORE calling the server side. By subscribing to the onclick javascript handler of the corresponding control that is raising the server side event to delete.

For example if you have a delete link:

<asp:LinkButton 
    ID="DeleteButton" 
    runat="server" 
    CausesValidation="False"
    CommandName="Delete" 
    Text="Delete"
    OnClientClick="return confirm('Are you sure you want to delete?');" 
/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM