Disable buttons on post back using jquery in .net app

后端 未结 7 2079
悲哀的现实
悲哀的现实 2020-12-20 17:03

I have a asp.net app that I want to disable the buttons as soon as they are clicked in order to prevent multiple submissions. I\'d like to use jquery for this as the site a

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-20 17:43

    Add this attribute to your button:

    usesubmitbehavior="False"
    

    This will insert something like the following into onclick:

    javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$Main$Tabs$SaveTab$Cancel", "", true, "", "", false, false))
    

    This code will cause a post back even if the button is disabled. Showing a confirmation dialog and allowing the post back to be cancelled gets a little more interesting:

        var click = $("[id$='_Cancel']")[0].onclick;
        $("[id$='_Cancel']")[0].onclick = null;
        $("[id$='_Cancel']").bind('click', function (event) { addFeeSchedule.onCancelClick(event) });
        $("[id$='_Cancel']").bind('click', click);
    

    In order to prevent the post back from occurring immediately, remove the onclick code inserted by .net and bind it after your own function using jQuery. Use event.stopImmediatePropagation(), to prevent the post back:

    onCancelClick: function (event) {
    
        var confirmResponse;
    
        confirmResponse = confirm('No fee schedule will be created.\n\nAre you sure you want to cancel?');
    
        if (confirmResponse == true) {
    
            showWait();
            event.target.disabled = 'true';
    
        } else {
    
            event.stopImmediatePropagation();
    
        }
    
    },
    

提交回复
热议问题