最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Raising Javascript confirm box from code behind - Stack Overflow

programmeradmin3浏览0评论

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?');" 
/>
发布评论

评论列表(0)

  1. 暂无评论