0

I have a DropDownList wrapped inside an UpdatePanel in ASP.net. When you select a different option it first calls function ShowLoading and then proceeds to call the code behind function 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?

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>
0

3 Answers 3

1

Remove onchange from DropDownList

<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

//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>
Sign up to request clarification or add additional context in comments.

1 Comment

@JohnDoe one thing I forgot to include in my answer, you have to remove onchange="ShowLoading()" code from element
0

Add return false at the end of your ShowLoading function. That should prevent the post back from triggering.

5 Comments

function ShowLoading() { return false; }
Alexander's method of capturing and removing the existing onchange method looks like it should work.
it doesn't work...could it have something to do with it being in an updatepanel?
I don't have enough reputation to comment directly on Alexander's answer. Put the first block of his code inside a pageLoad() { .. code ... } function. That is automatically called after an update panel refresh.
dont be so hard on yourself @Noah B. Just select 'add a comment'
0

return false if you don't want to execute server side code (code behind)

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()"

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.