ASP.NET在执行代码隐藏之前确认 [英] ASP.NET confirm before executing codebehind

查看:174
本文介绍了ASP.NET在执行代码隐藏之前确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户可以删除记录的表单,我想要一个弹出消息,用户必须单击确定以确认删除。

I have a form where a user can delete a record, and I want a pop up message where the user has to click okay to confirm the delete.

删除按钮:

<asp:Button ID="btnDelete" runat="server" Text="Delete" UseSubmitBehavior="false" OnClick="btnDelete_Click" OnClientClick="confirmation();" />

确认功能:

function confirmation() {
        var answer = confirm("Are you sure you want to delete? This action cannot be undone.")
    }

所以现在,点击删除按钮会在后面的代码中执行btnDelete_Click Sub,无论您是单击okay还是在弹出框中取消。我知道我可以在我的javascript函数中添加if(answer){ - 这里的一些代码 - },但是可以使用javascript从代码隐藏中执行代码吗?或者还有其他方法吗?

So right now, clicking the delete button executes the btnDelete_Click Sub in the code behind regardless of whether you click okay or cancel in the pop up box. I know I can add if (answer) { -- some code here -- } in my javascript function, but is it possible to use javascript to execute code from the codebehind? Or is there another way to do this?

推荐答案

请尝试以下操作。您必须返回确认函数的结果(true或false)。

Please try as follows. You have to return the result of the confirmation function (true or false).

<asp:Button 
    ID="btnDelete" 
    runat="server" 
    Text="Delete" 
    UseSubmitBehavior="false" 
    OnClick="btnDelete_Click" 
    OnClientClick="return confirmation();" />

 

function confirmation() {
    return confirm("Are you sure you want to delete?");
}

这篇关于ASP.NET在执行代码隐藏之前确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆