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

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==================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top