Thursday, February 26, 2009

Avoid Multiple Form Submits

Multiple form submits is a serious issue in web applications because it’ll result in unexpected behavior like multiple entries in database .I have spent some time for the same in Google and I got some solutions for that.
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 serious issue 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>

0 comments:

Post a Comment