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

Discussion in 'ASP .Net' started by Ghafran Abbas, Oct 12, 2004.

  1. 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==================
    Ghafran Abbas, Oct 12, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sylvia sil
    Replies:
    1
    Views:
    597
    Curt_C [MVP]
    Dec 29, 2004
  2. =?Utf-8?B?anVubGlh?=

    how to disable asp.net page auto post back?

    =?Utf-8?B?anVubGlh?=, May 20, 2005, in forum: ASP .Net
    Replies:
    9
    Views:
    13,804
    =?Utf-8?B?ZG90bmV0dGVzdGVy?=
    Aug 29, 2005
  3. =?Utf-8?B?Z29yaWxsYQ==?=
    Replies:
    1
    Views:
    754
    Masudur
    May 25, 2007
  4. Paul Oakfleet
    Replies:
    1
    Views:
    836
    Chris Riesbeck
    Sep 10, 2003
  5. Chuck
    Replies:
    0
    Views:
    249
    Chuck
    Nov 10, 2003
Loading...

Share This Page