gpt4 book ai didi

javascript - ASP.NET 使用 Javascript 停止 CodeBehind 调用

转载 作者:行者123 更新时间:2023-12-03 00:03:45 27 4
gpt4 key购买 nike

我有一个 DropDownList 包裹在 ASP.net 中的 UpdatePanel 内。当您选择不同的选项时,它首先调用函数 ShowLoading,然后继续调用函数 dropDownMonth_SelectedIndexChanged 背后的代码。我想让它做这个。但是 - 是否有办法保持结构,但是当它遇到 javascript 函数时,我可以编写一些 javascript 代码来阻止它调用后面的代码?

function ShowLoading() {

}

<asp:DropDownList CssClass="form-control" ID="dropDownMonth" runat="server" AutoPostBack="true" ClientIDMode="Static" Style="display: none" onchange="ShowLoading()" OnSelectedIndexChanged="dropDownMonth_SelectedIndexChanged"></asp:DropDownList>

最佳答案

DropDownList 中删除 onchange

<asp:DropDownList CssClass="form-control" ID="dropDownMonth" runat="server" AutoPostBack="true" ClientIDMode="Static" Style="display: none" OnSelectedIndexChanged="dropDownMonth_SelectedIndexChanged"></asp:DropDownList>

并重写生成的 onchange 处理程序,如下所示

//make sure this code is executed when page is loaded
<script>
var dropdown = document.getElementById("dropDownMonth");
var onchange = dropdown.onchange;
dropdown.removeAttribute("onchange"); //removing original onchange handler
var newonchange = function (ev) {
if (ShowLoading()) {
onchange(ev);
}
}
dropdown.onchange = newonchange;

function ShowLoading() {
var postBack = /*condition to postback*/;
//..
return postBack;
}


</script>

关于javascript - ASP.NET 使用 Javascript 停止 CodeBehind 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55070783/

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