C#:根据确认对话框阻止 AutoPostBack

发布于 2024-09-13 04:52:09 字数 389 浏览 13 评论 0原文

我想在 asp:DropDownList 中选择特定值时显示确认对话框。如果确认对话框返回 false(取消),那么我想阻止 AutoPostBack。

<asp:DropDownList id="theDropDownID" onchange="foo()"></asp:DropDownList>

但是,它忽略 foo() 返回的值并实际执行回发。
onchange事件的生成代码为:

foo(); setTimeout("__doPostBack('theDropDownID','')", 0);

所以基本上控制 .net 添加的 setTimeout 就可以完成这项工作。

知道怎么做吗?
谢谢!

I want to show a confirmation dialog when a specific value is selected in an asp:DropDownList. If the confirmation dialog returns false (cancel) then I want to prevent the AutoPostBack.

<asp:DropDownList id="theDropDownID" onchange="foo()"></asp:DropDownList>

However, it ignores the returned value from foo() and actually does the postback.
The generated code of the onchange event is:

foo();
setTimeout("__doPostBack('theDropDownID','')", 0);

so basically controlling the setTimeout that the .net adds, will do the job.

Any idea how?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不吻晚风 2024-09-20 04:52:09

要么

onchange="return foo();"

function foo() {
    //do whatever
    return false;
}

或者使用下面的 jQuery

  $(function () {   //equivallent to: $(document).ready(function() {
        $('#theDropDownID').removeAttr('onchange');  
        $('#theDropDownID').change(function(e) {  
            e.preventDefault();  
            if (confirm("do postback?")) {
             setTimeout('__doPostBack(\'theDropDownID\',\'\')', 0);
            }
        });
   });

我更喜欢第二种方法,因为它是不引人注目的 javascript:所有行为都在一起并且根本没有内联 javascript。

Either

onchange="return foo();"

function foo() {
    //do whatever
    return false;
}

Or use the following jQuery

  $(function () {   //equivallent to: $(document).ready(function() {
        $('#theDropDownID').removeAttr('onchange');  
        $('#theDropDownID').change(function(e) {  
            e.preventDefault();  
            if (confirm("do postback?")) {
             setTimeout('__doPostBack(\'theDropDownID\',\'\')', 0);
            }
        });
   });

I prefer the second approach because it is unobtrusive javascript: all of the behaviour is together and there is no inline javascript at all.

暗喜 2024-09-20 04:52:09

尝试将 onchange 值设置为:

onchange="return foo();"

这应该可以正常完成。

Try setting the onchange value to:

onchange="return foo();"

that should do the trick normally.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文