Disable Submit Button on Post back and On Submit in ASP.net

  • Thread starter Thread starter Ghafran Abbas
  • Start date Start date
G

Ghafran Abbas

Call this function from the Page_OnLoad event of your asp.net page.
This will prevent the user from doing multiple post backs or button
clicks.
This was designed to not disable the button, because asp.net 1.x will
not pick up the button click event.
Instead, it monitors the on submit event of the form and the
_doPostBack event of the asp.net page.
It overrides the _doPostBack and onsubmit events and checks whether
the page has already been submitted or posted back.
If the user does anything during a submit or postback, it is ignored.

The code below is using a submit button named "btnNext"; you can
change the code to reference your own button.


============Begin Code==================

Public Function PutOnSubmitButtonDisabler(ByRef oPage As Page)

If Not oPage.IsClientScriptBlockRegistered("PleaseWait") Then
oPage.RegisterClientScriptBlock("PleaseWait", "<script
type='text/javascript'>" & vbCrLf & _
"var PageIsProcessing = false;" & vbCrLf & _
"function PleaseWait(){" & vbCrLf & _
"if(Page_IsValid){" & vbCrLf & _
"if(PageIsProcessing==true){" & vbCrLf & _
"return false;" & vbCrLf & _
"}else{" & vbCrLf & _
"if(document.forms[0].btnNext !=
null){document.forms[0].btnNext.value = 'Processing...';}" & vbCrLf &
_
"PageIsProcessing = true;" & vbCrLf & _
"return true;" & vbCrLf & _
"}" & vbCrLf & _
"}" & vbCrLf & _
"}" & vbCrLf & _
"</script>")
End If

oPage.RegisterStartupScript("PleaseWaitOnPostBack", "<script
type='text/javascript'>" & vbCrLf & _
"var __doPostBack__ = window.__doPostBack;" & vbCrLf & _
"window.__doPostBack = function(eventTarget, eventArgument) {" &
vbCrLf & _
"if(PageIsProcessing==true){" & vbCrLf & _
"}else{" & vbCrLf & _
"if(document.forms[0].btnNext !=
null){document.forms[0].btnNext.value = 'Processing...';}" & vbCrLf &
_
"__doPostBack__(eventTarget, eventArgument);" & vbCrLf & _
"PageIsProcessing = true;" & vbCrLf & _
"}" & vbCrLf & _
"}" & vbCrLf & _
"</script>")
oPage.RegisterOnSubmitStatement("PleaseWait",
"if(!PleaseWait()){return false};")
End Function

============End Code==================
 
This assumes the browser has javascript enabled. How do you prevent multiple
submits in a purely server side way with .net?
 
You can use a variable in the user's session:
http://www.daveandal.net/books/6744/buttons/one-click.aspx

Stephen J. Kladich said:
This assumes the browser has javascript enabled. How do you prevent multiple
submits in a purely server side way with .net?

Ghafran Abbas said:
Call this function from the Page_OnLoad event of your asp.net page.
This will prevent the user from doing multiple post backs or button
clicks.
This was designed to not disable the button, because asp.net 1.x will
not pick up the button click event.
Instead, it monitors the on submit event of the form and the
_doPostBack event of the asp.net page.
It overrides the _doPostBack and onsubmit events and checks whether
the page has already been submitted or posted back.
If the user does anything during a submit or postback, it is ignored.

The code below is using a submit button named "btnNext"; you can
change the code to reference your own button.


============Begin Code==================

Public Function PutOnSubmitButtonDisabler(ByRef oPage As Page)

If Not oPage.IsClientScriptBlockRegistered("PleaseWait") Then
oPage.RegisterClientScriptBlock("PleaseWait", "<script
type='text/javascript'>" & vbCrLf & _
"var PageIsProcessing = false;" & vbCrLf & _
"function PleaseWait(){" & vbCrLf & _
"if(Page_IsValid){" & vbCrLf & _
"if(PageIsProcessing==true){" & vbCrLf & _
"return false;" & vbCrLf & _
"}else{" & vbCrLf & _
"if(document.forms[0].btnNext !=
null){document.forms[0].btnNext.value = 'Processing...';}" & vbCrLf &
_
"PageIsProcessing = true;" & vbCrLf & _
"return true;" & vbCrLf & _
"}" & vbCrLf & _
"}" & vbCrLf & _
"}" & vbCrLf & _
"</script>")
End If

oPage.RegisterStartupScript("PleaseWaitOnPostBack", "<script
type='text/javascript'>" & vbCrLf & _
"var __doPostBack__ = window.__doPostBack;" & vbCrLf & _
"window.__doPostBack = function(eventTarget, eventArgument) {" &
vbCrLf & _
"if(PageIsProcessing==true){" & vbCrLf & _
"}else{" & vbCrLf & _
"if(document.forms[0].btnNext !=
null){document.forms[0].btnNext.value = 'Processing...';}" & vbCrLf &
_
"__doPostBack__(eventTarget, eventArgument);" & vbCrLf & _
"PageIsProcessing = true;" & vbCrLf & _
"}" & vbCrLf & _
"}" & vbCrLf & _
"</script>")
oPage.RegisterOnSubmitStatement("PleaseWait",
"if(!PleaseWait()){return false};")
End Function

============End Code==================
 
Back
Top