简体   繁体   中英

Prevent double click in submit-button when postback from other controls executed before button click

I got a problem in preventing double click of Submit button in ASP.NET . I can disable the submit-button in pages which don't have ASP.NET Calendar . In pages which have this Calendar , calendar's postback disables the submit-button before the button is clicked.

I used the following javascript to disable the button in button-click. It works if there is no postback from other asp.net-controls before the submit-button is clicked.

function DisableButton() {
    document.getElementById("<%=ibtnNext.ClientID %>").disabled = true;
}
window.onbeforeunload = DisableButton;

i don't know what to do. All suggestions are welcome.

Instead of using the window.onbeforeunload event. Disable the button on its OnClientClick event.

The button's postBack(onClick event) will get executed only after the OnClientClick event in javascript.

<button id="ibtnNext" OnClientClick="return DisableButton()" onclick="btnSubmit_Click">Copy Text</button>

<script>
function DisableButton() 
{
     document.getElementById("<%=ibtnNext.ClientID %>").disabled = true;

     return true;
}
</script>

Thanks all, i got solution now as the following

javascript <script type="text/javascript"> function DisableButton(btn){ btn.disabled = true; } </script> <script type="text/javascript"> function DisableButton(btn){ btn.disabled = true; } </script>

in page load

protected sub Page_load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim optionsSubmit As New PostBackOptions(ibtnNext)
    ibtnNext.OnClientClick = "DisableButton(this);"
    ibtnNext.OnClientClick += ClientScript.GetPostBackEventReference(optionsSubmit) End Sub

in click event

Protected Sub ibtnNext_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibtnNext.Click
    System.Threading.Thread.Sleep(2000) End Sub

original link http://www.devtoolshed.com/content/disable-button-submit-prevent-multiple-submits

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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