admin管理员组

文章数量:1317898

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.

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.

Share Improve this question edited Jul 26, 2013 at 14:56 dda 6,2132 gold badges27 silver badges35 bronze badges asked Jul 26, 2013 at 14:46 Sai AvinashSai Avinash 4,75317 gold badges62 silver badges98 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

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?');" 
/>

本文标签: cRaising Javascript confirm box from code behindStack Overflow