gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:46:06 24 4
gpt4 key购买 nike

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

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

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

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

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

知道怎么做了吗?
谢谢!

最佳答案

要么

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。

关于C#:根据确认对话框防止 AutoPostBack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3415758/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com