在确认弹出窗口时从javascript取消回发 [英] cancel postback from javascript on confirm popup

查看:73
本文介绍了在确认弹出窗口时从javascript取消回发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出现此问题之后 ASP.NET:OnServerClick事件如果使用onclick不会调用处理程序

我实现了一种解决方法:

I implemented a workaround:

<button id="idBtnPrint" runat="server" type="submit" onserverclick="BtnPrint_Click" onclick="confirmImpression();">print</button>

javascript函数:

javascript function:

    function confirmImpression() {

        if (!confirm("sure ?"))
            arg.whatever;

    }

如果确认为肯定,则取消运行不执行(如我所愿)时,将开始回发,这仅是因为生成的错误js未定义arg"(正常,因为arg未实例化)脚本已锁定,因此没有回发(我也想).

where confirmation is positive the postback is started when cancel the postback is not run (as I want) simply because an error generated js "arg is undefined" (normal because arg is not instanciate) script is locked and therefore no postback (as I want too).

,但如何正确执行此操作.我不会在浏览器的状态栏中看到该错误.

but how to do this properly. I would not see the error appear in the status bar in the browser.

欢迎任何建议!

谢谢

推荐答案

简单地将确认包装在if块中,如果确认失败,则返回false.生成回发的标准javascript将会附加到检查中,因此如果确认操作,则可以运行.

Simply wrap the confirmation in an if-block and return false if the confirmation fails. The standard javascript that generates the postback will be appended to the check and thus be run if the action is confirmed.

<button id="idBtnPrint" runat="server" type="submit"
        onserverclick="BtnPrint_Click"
        onclientclick="if (!confirm('sure ?')) { return false; }">print</button>

未定义 arg 的原因是,您不将其用作参数,也未在window对象中对其进行定义.我假设您正在尝试加入验证系统,但是在这种情况下它将无法正常工作,因为未调用验证逻辑.如果用户回答否定,并且上面的代码将执行此操作,您只是想停止执行客户端处理程序.

The reason that arg is undefined is that you don't use it as an argument nor is it defined in the window object. I assume that you're trying to hook into the validation system, but it won't work in this case because the validation logic isn't being invoked. You simply want to stop execution of the client-side handler if the user answers in the negative and the above code will do that.

尽管所提问题的答案也可以使用,但我更喜欢使用完整的javascript语句来帮助理解发生的情况.

Though the answer in the referenced question will work as well I prefer to use a full javascript statement to aid in understanding what is going on.

这篇关于在确认弹出窗口时从javascript取消回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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