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

标签 javascript asp.net

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

相关文章:

asp.net - 将 .Net Core 转换为 .Net Framework

c# - 检测 AutoCompleteExtender 选择事件

javascript - 用子组件替换父组件

javascript - AngularJs-在指令之间共享对象数组

javascript - 谷歌脚本中的indexOf

javascript - 使用 jquery 向上或向下滑动整个 HTML 表格,不起作用

asp.net - 如何在 ASP.Net Core 中设置全局化文化?

javascript - 使用 View 框时在 Raphael.js 中拖放一组路径

asp.net - 使用 ASP.Net 2.0 创建 SOAP 请求

javascript - 我想将 javascript 中的数据和文件发送到 Controller (但如何)(Mvc asp.net)