Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Avoid Multiple Form Submits

0.00/5 (No votes)
6 Aug 2009 1  
Multiple form submits can result difficulties in web applications because of unexpected behaviors like multiple entries in database. Here is a

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Multiple form submits can result difficulties in web applications because of unexpected behaviors like multiple entries in database. Here is a solution to help you avoid multiple submits.


If you are submitting the page only once then you can use,

<form onsubmit="return Submit();">
And the method is like,
<script type="text/javascript">
var flag = false;

function Submit()
{
if (flag)
{
return false;
}
else
{
flag = true;
return true;
}
}
<
/script>


For a single button you can use,
btnSubmit.Attributes["onclick"] = "this.disabled=true;" +
GetPostBackEventReference(btnSubmit);
For pages with update panels multiple submit is a real problem as page is posting asynchronously or partially.In that scenario you can use Sys.WebForms.PageRequestManager for fixing the issue,
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

<script type="text/javascript">

Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequest);

function BeginRequest(sender, e)
{
e.get_postBackElement().disabled = true;
}

</script>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here