воскресенье, 30 августа 2009 г.

Avoid Multiple Form Submits

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;
flag = true;
return true;

For a single button you can use,
btnSubmit.Attributes["onclick"] = "this.disabled=true;" +
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">

<script type="text/javascript">


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


Комментариев нет: