469,621 Members | 1,666 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,621 developers. It's quick & easy.

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

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==================
Nov 18 '05 #1
2 8680
This assumes the browser has javascript enabled. How do you prevent multiple
submits in a purely server side way with .net?

"Ghafran Abbas" wrote:
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==================

Nov 19 '05 #2
You can use a variable in the user's session:
http://www.daveandal.net/books/6744/...one-click.aspx

"Stephen J. Kladich" <St*************@discussions.microsoft.com> wrote in
message news:F2**********************************@microsof t.com...
This assumes the browser has javascript enabled. How do you prevent multiple submits in a purely server side way with .net?

"Ghafran Abbas" wrote:
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==================

Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Bruno Alexandre | last post: by
14 posts views Thread by Sinity | last post: by
3 posts views Thread by Jeff | last post: by
8 posts views Thread by rodchar | last post: by
16 posts views Thread by Barry Gilmore | last post: by
5 posts views Thread by Sehboo | last post: by
6 posts views Thread by =?Utf-8?B?Unlhbg==?= | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.