简体   繁体   English

ASP.NET使用Java脚本停止代码隐藏

[英]ASP.NET Use Javascript to Stop CodeBehind Call

I have a DropDownList wrapped inside an UpdatePanel in ASP.net. 我在ASP.net的UpdatePanel中包装了一个DropDownList When you select a different option it first calls function ShowLoading and then proceeds to call the code behind function dropDownMonth_SelectedIndexChanged . 当您选择其他选项时,它首先调用ShowLoading函数,然后继续调用dropDownMonth_SelectedIndexChanged函数后面的代码。 I want it to do this. 我要它做到这一点。 But - is there anyway to keep the structure but when it hits the javascript function there is some javascript code I can write that prevents it from calling the code behind? 但是-是否仍然可以保留结构,但是当它到达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>

Remove onchange from DropDownList DropDownList移除onchange

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

And override generated onchange handler like this 并像这样覆盖生成的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>

Add return false at the end of your ShowLoading function. 在ShowLoading函数的末尾添加return false。 That should prevent the post back from triggering. 那应该防止回发触发。

return false if you don't want to execute server side code (code behind) 如果您不想执行服务器端代码(后面的代码),则返回false

function ShowLoading() {

    // return true; // Will continue to call code behind

    return false; // Stop and don't call code behind
}

and use return when calling the JS function onchange="return ShowLoading()" 并在调用JS函数onchange="return ShowLoading()"时使用return

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM