我有一个 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/