Friday, October 12, 2007

Avoid multiple submits during the ongoing postback

To Avoid multiple submits during the ongoing postbacks
Just think of an ASP.Net web page that contains a button control which initiates some time consuming task. Upon clicking that button, the page submit will be initiated. In the mean time, the button will be available for further clicking. This is common in most of the heavy duty applications / web pages.
Some sort of work around that we can immediately think against this is, disabling the button control to avoid further clicks.
To achieve this,
Need to implement some JavaScript related stuffs.
1. Create a javascript function to Find and disable the specific control(s) (Button in most of the cases). The function should return “false”.

<script type="text/javascript">
function DisableControlOnSubmit()
{
var buttonToBeDisabled = document.getElementById("SubmitButton");
buttonToBeDisabled.disabled = true;
return false;
}
</script>

2. Call the function in the onsubmit event handler of the form.

<form id="form1" runat="server" onsubmit=" DisableControlOnSubmit()">

3. In ASP.Net 2.0, set the SubmitDisabledControls attribute of the
tag to true.

<form id="form1"…………………… submitdisabledcontrols="true">

4. Set the “UseSubmitBehavior” attribute of the Submit button to “false”.

<asp:Button ……………UseSubmitBehavior="false" />

No comments:

Post a Comment

Creative Commons License
This work by Tito is licensed under a Creative Commons Attribution 3.0 Unported License.